我想实现以下目标:
一个活动开始时没有ClickListener,并且有四个文本视图,所有文本视图的背景都是白色。
我想将textview 1的颜色更改为蓝色。等待2秒然后将其改回白色并将textview 2更改为蓝色。等待2秒然后将其改回白色...以此类推,直到我将textview 4变为蓝色并变成白色。
完成后,我想添加ClickListener并等待用户输入。
如何实现这个目标?我是Android新手,但了解一些相关知识。
没有必要为此创建线程或动画。
解决方案非常简单:使用Handler.postDelayed()或Handler.sendMessageDelayed():
http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long) http://developer.android.com/reference/android/os/Handler.html#sendMessageDelayed(android.os.Message, long)
为了实现健壮的实现,请确保在Activity.onDestroy()之前删除任何挂起的消息。 (或者,如果您在Activity.onStart()中发布它们,请在Activity.onStop()中删除它们;如果在Activity.onResume()中发布,请在Activity.onPause()中删除。)
我有一个样例可以用线程和handleMessage来完成这个任务
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.widget.EditText;
import android.widget.TextView;
public class l15_threadOneaacto extends Activity {
/** Called when the activity is first created. */
TextView tv[]=new TextView[4];
EditText et;
Thread bcko;
static int index=0;
boolean isRunning=false;
boolean acceptevent=false;
Handler hn=new Handler(){
@Override
public void handleMessage(android.os.Message msg) {
switch (index) {
case 0: tv[0].setBackgroundColor(Color.BLUE); break;
case 1: tv[0].setBackgroundColor(Color.WHITE); break;
case 2: tv[1].setBackgroundColor(Color.BLUE); break;
case 3: tv[1].setBackgroundColor(Color.WHITE); break;
case 4: tv[2].setBackgroundColor(Color.BLUE); break;
case 5: tv[2].setBackgroundColor(Color.WHITE); break;
case 6: tv[3].setBackgroundColor(Color.BLUE); break;
case 7: tv[3].setBackgroundColor(Color.WHITE); break;
}
index++;
if(index==8){
acceptevent=true;
et=(EditText)findViewById(R.id.bbb);
et.setText("ready for input");
}
};
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv[0]=(TextView)findViewById(R.id.tx1);
tv[1]=(TextView)findViewById(R.id.tx2);
tv[2]=(TextView)findViewById(R.id.tx3);
tv[3]=(TextView)findViewById(R.id.tx4);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
bcko=new Thread (new Runnable() {
@Override
public void run() {
try {
//
for(int i=0;i<8 && isRunning;i++){
Thread.sleep(2000);
hn.sendMessage(hn.obtainMessage());
}
} catch (Exception e) {
// TODO: handle exception
}
}
});
isRunning=true;
bcko.start();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
isRunning=false;
}
}
kayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tx1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:text=""
/>
<TextView
android:id="@+id/tx2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:text=""
/>
<TextView
android:id="@+id/tx3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:text=""
/>
<TextView
android:id="@+id/tx4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:text=""
/>
<EditText
android:id="@+id/bbb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="not ready "
/>
</LinearLayout>
祝你好运,