Android——线程+最简单计数器
来源:互联网 发布:李晨爷爷 知乎 编辑:程序博客网 时间:2024/05/15 07:38
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>
0 0
- Android——线程+最简单计数器
- java线程使用示例——最简单的线程
- C#组件开发——简单计数器
- 最简单的ASP网页计数器源代码
- 做的最简单的APP-计数器
- Android消息机制—倒计时计数器
- android 启动一个最简单的 activity 时的线程
- 简单分频原理与实现——计数器
- 简单计数器
- 简单计数器
- 最简单的远程线程
- 线程池最简单用法
- 最简单的线程池
- c# 最简单的线程
- 一个最简单的线程
- android 计数器
- Android最简单的数据传递——用户注册
- Android上最简单的IPC方案——Messenger
- 堆和栈基本知识
- 应用层协议
- linux下自定义USB HID设备
- android分析windowManager、window、viewGroup之间关系(一)
- POJ 1502 MPI Maelstrom
- Android——线程+最简单计数器
- 151.leetcode Reverse Words in a String(medium)[字符串分词翻转]
- 实现Runnable接口比继承Thread类所具有的优势
- VC----初识MFC框架
- 534 Policy requires SSL错误
- hdu-4348-To the moon-离线处理 or 主席树区间更新
- Ubuntu14.04 + Caffe + OpenCV 3.1 + Ffmpeg + Anaconda + Cuda8.0rc安装
- BZOJ1044——[HAOI2008]木棍分割
- C#使用Litjson构建json数据