Android——线程+最简单计数器
1.第一种方法
<span style="font-size:18px;">package com.example.jer824;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv;
private TextView tv1;
int a=100;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1){
Log.d("??????",a+"");
tv.setText(a+"");
a--;
Log.d("??????","成功");
new GetCache(a).run();
}else{tv.setText("倒计时结束");}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
tv1=(TextView)findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new GetCache(a).run();
}
});
}
private class GetCache implements Runnable { //两种方法继承或接口
private int id;
public GetCache(int id) {
this.id = id;
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = new Message();
if (id==0) {
message.what = 2;
handler.sendMessage(message);
}else {
message.what = 1;
handler.sendMessage(message);
}
}
}
}</span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jer824.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="100"
android:textSize="100sp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/tv"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="计时器"
android:textSize="100sp"
android:clickable="true"
android:id="@+id/tv1"
android:textStyle="bold"
android:gravity="center"
android:layout_below="@+id/tv"
/>
</RelativeLayout></span><span style="font-size:24px;">
</span>
2.第二种方法
<span style="font-size:18px;">package com.example.jer824;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
private TextView tv;
private Button bt;
//线程间通信
private android.os.Handler handler=new android.os.Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
tv.setText(msg.what+"");
Bundle bundle=msg.getData();
String str=bundle.getString("a");
tv.setText(str+msg.what);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv= (TextView) findViewById(R.id.tv);
bt= (Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new GetCache()).start();
// new Thread(new Runnable() {
// @Override
// public void run() {
// int a=100;
// while (a>=0){
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// a--;
// Message message=new Message();
// message.what = a;
// handler.sendMessage(message);
// }
// }
// }).start();
}
});
}
//多线程 子线程
//public class GetCache extends Thread
public class GetCache implements Runnable{
@Override
public void run() {
//子线程运行完了,去主线程
int a=100;
while (a>=0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
a--;
Message message=new Message();
message.what = a;
Bundle bundle=new Bundle();
bundle.putString("a","AAA");
message.setData(bundle);
// handler.sendMessageDelayed(message,200);
handler.sendMessage(message);
//与上面三行一个意思
// handler.sendEmptyMessage(a);
}
}
}
}
</span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.jer824.Main2Activity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="100"
android:textSize="40sp"
android:textStyle="bold"
android:gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/tv"
android:text="开始计时"
android:id="@+id/bt"
/>
</RelativeLayout></span><span style="font-size:24px;">
</span>
3.第三种方法
<span style="font-size:18px;">package com.example.jer824;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main3Activity extends AppCompatActivity {
private TextView tv;
private Button bt;
private int count=100;
//线程间通信
private android.os.Handler handler=new android.os.Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//tv.setText(msg.what);
if(count==0){
return;
}
tv.setText(msg.what + "");
handler.sendEmptyMessageDelayed(count--,500);
//tv.setText(msg.what + "");
//bundle传值
// Bundle bundle=msg.getData();
// String str = bundle.getString("a");
// tv.setText(str+msg.what);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv= (TextView) findViewById(R.id.tv);
bt= (Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(count);
//
// new Thread(new Runnable() {
// @Override
// public void run() {
// int a=100;
// while (a>0){
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// a--;
// // tv.setText(a+"");
// Message message=new Message();
// message.what = a;
// handler.sendMessage(message);
// }
// }
// }).start();
//new Thread(new GetCache()).start();
}
});
}
//多线程 子线程
//public class GetCache extends Thread
public class GetCache implements Runnable{
@Override
public void run() {
//子线程运行完了,去主线程
int a=100;
while (a>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
a--;
// tv.setText(a+"");
Message message=new Message();
message.what = a;
//加入bundle值
// Bundle bundle=new Bundle();
// bundle.putString("a","1");
// message.setData(bundle);
handler.sendMessage(message);
//多久后启动
//handler.sendMessageAtTime(message,500);
//handler.sendMessageDelayed(message,500);
//handler.sendEmptyMessage(a);传空值
}
}
}
}
</span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.jer824.Main2Activity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="100"
android:textSize="40sp"
android:textStyle="bold"
android:gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/tv"
android:text="开始计时"
android:id="@+id/bt"
/>
</RelativeLayout>
</span>