[android]代码库
package com.example.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private ProgressBar progressBar;
private SeekBar seekBar;
private Button button1, button2, button3, button4, button5, button6;
int i = 0;
int j = 0;
boolean flag1 = true;
boolean flag2 = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
seekBar = (SeekBar) findViewById(R.id.seekBar);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button1.setOnClickListener(new button());
button2.setOnClickListener(new button());
button3.setOnClickListener(new button());
button4.setOnClickListener(new button());
button5.setOnClickListener(new button());
button6.setOnClickListener(new button());
/**
* 拖动seekbar时间
*/
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
/**
* seekbar拖动后的位置为第二个参数 arg1
*/
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
j=arg1;
}
});
}
/**
* 按钮点击事件
*
* @author Administrator
*
*/
class button implements OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button1:
if (flag1) {
handler.post(rannable);
flag1 = false;
}
break;
case R.id.button2:
flag1 = true;
handler.removeCallbacks(rannable);
break;
case R.id.button3:
flag1 = true;
handler.removeCallbacks(rannable);
i = 0;
progressBar.setProgress(i);
break;
case R.id.button4:
if (flag2) {
handler2.post(r);
flag2 = false;
}
break;
case R.id.button5:
flag2 = true;
handler2.removeCallbacks(r);
break;
case R.id.button6:
flag2 = true;
handler2.removeCallbacks(r);
j = 0;
seekBar.setProgress(j);
break;
default:
break;
}
}
}
/**
* 线程1
*/
Handler handler = new Handler();
Runnable rannable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
i++;
progressBar.setProgress(i);
handler.postDelayed(rannable, 100);
if (i == 100) {
i = 0;
}
System.out.println("ddd");
}
};
/**
* 线程2
*/
Handler handler2 = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
j++;
seekBar.setProgress(j);
handler2.postDelayed(r, 100);
if (j == 100) {
j = 0;
}
System.out.println("ooo");
}
};
/**
* 菜单
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
[代码运行效果截图]