handler 重新整理介面,執行緒傳值
阿新 • • 發佈:2019-02-12
package com.example.button_test;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button;
private int i = 60;
private final static int SHOW_TOAST = 0x011;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setTextSize(25);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.post(runnable);
Toast.makeText(MainActivity.this, "dianj", Toast.LENGTH_SHORT).show();
//帶值傳遞
/*
* 值 detailList_paging = 值();
* msg.what = INTEGRALDETAIL_DETAIL_REFRESH;
msg.obj = detailList_paging;
handler.sendMessage(msg);
*/
}
});
}
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(SHOW_TOAST);
//開始
handler.postDelayed(runnable, 1000);
//停止
// handler.removeCallbacks(runnable);
}
};
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what){
case SHOW_TOAST:
button.setText("這是測試倒計時"+i);
i=i-1;
break;
//有值
/*
*
* case SHOW_TOAST:
* 值 detailList_paging = (值)msg.obj;
*
*/
default:
break;
}
}
};
}
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button;
private int i = 60;
private final static int SHOW_TOAST = 0x011;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setTextSize(25);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.post(runnable);
Toast.makeText(MainActivity.this, "dianj", Toast.LENGTH_SHORT).show();
//帶值傳遞
/*
* 值 detailList_paging = 值();
* msg.what = INTEGRALDETAIL_DETAIL_REFRESH;
msg.obj = detailList_paging;
handler.sendMessage(msg);
*/
}
});
}
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(SHOW_TOAST);
//開始
handler.postDelayed(runnable, 1000);
//停止
// handler.removeCallbacks(runnable);
}
};
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what){
case SHOW_TOAST:
button.setText("這是測試倒計時"+i);
i=i-1;
break;
//有值
/*
*
* case SHOW_TOAST:
* 值 detailList_paging = (值)msg.obj;
*
*/
default:
break;
}
}
};
}