layout 文件 如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/tv1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/tv2"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/tv3"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/tv4"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/tv5"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NEXT"
android:id="@+id/bt1"/></LinearLayout>
java 代码如下:package com.src.marquee;import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MarqueeActivity extends Activity implements Runnable { private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private TextView tv5;
private Button bt1; private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marquee);
tv1 = (TextView) this.findViewById(R.id.tv1);
tv2 = (TextView) this.findViewById(R.id.tv2);
tv3 = (TextView) this.findViewById(R.id.tv3);
tv4 = (TextView) this.findViewById(R.id.tv4);
tv5 = (TextView) this.findViewById(R.id.tv5);
bt1 = (Button) this.findViewById(R.id.bt1); bt1.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Thread thread = new Thread(MarqueeActivity.this);
thread.start();
}
}); handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
tv1.setBackgroundColor(Color.RED);
tv5.setBackgroundColor(Color.WHITE);
break;
case 2:
tv2.setBackgroundColor(Color.RED);
tv1.setBackgroundColor(Color.WHITE);
break;
case 3:
tv3.setBackgroundColor(Color.RED);
tv2.setBackgroundColor(Color.WHITE);
break;
case 4:
tv4.setBackgroundColor(Color.RED);
tv3.setBackgroundColor(Color.WHITE);
break;
case 5:
tv5.setBackgroundColor(Color.RED);
tv1.setBackgroundColor(Color.WHITE);
break;
default:
break;
}
}
};
} @Override
public void run() {
Message msg = new Message();
int flag = 1;
while (true) {
msg.what = flag;
handler.sendMessage(msg);
flag++;
if(flag==6){
flag=1;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_marquee, menu);
return true;
}}
androidThreadHandlerMessage
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/tv1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/tv2"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/tv3"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/tv4"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/tv5"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NEXT"
android:id="@+id/bt1"/></LinearLayout>
java 代码如下:package com.src.marquee;import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MarqueeActivity extends Activity implements Runnable { private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private TextView tv5;
private Button bt1; private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marquee);
tv1 = (TextView) this.findViewById(R.id.tv1);
tv2 = (TextView) this.findViewById(R.id.tv2);
tv3 = (TextView) this.findViewById(R.id.tv3);
tv4 = (TextView) this.findViewById(R.id.tv4);
tv5 = (TextView) this.findViewById(R.id.tv5);
bt1 = (Button) this.findViewById(R.id.bt1); bt1.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Thread thread = new Thread(MarqueeActivity.this);
thread.start();
}
}); handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
tv1.setBackgroundColor(Color.RED);
tv5.setBackgroundColor(Color.WHITE);
break;
case 2:
tv2.setBackgroundColor(Color.RED);
tv1.setBackgroundColor(Color.WHITE);
break;
case 3:
tv3.setBackgroundColor(Color.RED);
tv2.setBackgroundColor(Color.WHITE);
break;
case 4:
tv4.setBackgroundColor(Color.RED);
tv3.setBackgroundColor(Color.WHITE);
break;
case 5:
tv5.setBackgroundColor(Color.RED);
tv1.setBackgroundColor(Color.WHITE);
break;
default:
break;
}
}
};
} @Override
public void run() {
Message msg = new Message();
int flag = 1;
while (true) {
msg.what = flag;
handler.sendMessage(msg);
flag++;
if(flag==6){
flag=1;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_marquee, menu);
return true;
}}
androidThreadHandlerMessage
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货