实现文字隔3秒自动循环变化

xml布局文件

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25sp"
android:text="nihaoma,wo yidain dou bu hao"
/>

MainActivity页面:

public class MainActivity extends Activity implements Runnable{
private TextView text;
private Handler handler;
private String[] title=new String[]{"000","111","222","333"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.text);
Thread t=new Thread(this);
t.start();
handler=new Handler(){
public void handleMessage(Message msg) {
if (msg.what==0x101) {
text.setText(msg.getData().getString("title"));

}
};
};

}
@Override
public void run() {
// TODO Auto-generated method stub
int index=0;
while (!Thread.currentThread().isInterrupted()) {
if (index<3) {
index++;
}else{
index=0;
}
Message m=handler.obtainMessage();
m.arg1=index;
Bundle bundle=new Bundle();
m.what=0x101;
bundle.putString("title", title[index]);
m.setData(bundle);
handler.sendMessage(m);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}

原文地址:https://www.cnblogs.com/xiaoshumiao/p/6834711.html