首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

TextSwitcher运用示例

2013-01-17 
TextSwitcher使用示例mainActivity如下:package c.cimport android.app.Activityimport android.os.Bund

TextSwitcher使用示例

mainActivity如下:

package c.c;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AnimationUtils;import android.widget.TextSwitcher;import android.widget.TextView;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity {    private TextSwitcher mIextSwitcher;    private String texts [];    private int textIndex=0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        init();    }   private void init(){   texts=new String[]{"空山新雨后","天气晚来秋","明月松间照","清泉石上流"};   mIextSwitcher=(TextSwitcher) findViewById(R.id.textSwitcher);   //设置用于显示文字的TextView   mIextSwitcher.setFactory(new ViewFactory() {public View makeView() {TextView textView=new TextView(MainActivity.this);textView.setTextSize(30);return textView; }   });   //处理点击事件   mIextSwitcher.setOnClickListener(new ClickListenerImpl());   //设置文字进入和退出动画   mIextSwitcher.setInAnimation   (AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));   mIextSwitcher.setOutAnimation   (AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));   //设置初始时显示的文字   mIextSwitcher.setText(texts[textIndex]);   }private class ClickListenerImpl implements OnClickListener {public void onClick(View v) {textIndex++;if (textIndex == texts.length) {textIndex = 0;} mIextSwitcher.setText(texts[textIndex]);}}}


main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >   <TextSwitcher       android:id="@+id/textSwitcher"        android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_centerInParent="true"   /></RelativeLayout>


 

热点排行