1.activity 代码
package com.test.org.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.os.Message;
public class MainActivity extends Activity {
private TextView mTextView01;
private Button mButton01;
private ProgressBar mProgressBar01;
public int intCounter = 0;
//自定义Handle信息代码用做别的识别
protected static final int GUI_STOP_NOTIFIER = 0x108;
protected static final int GUI_THREADING_NOTIFIER = 0x109;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton01 = (Button)findViewById(R.id.myButton1);
mTextView01 = (TextView)findViewById(R.id.myTextView1);
mProgressBar01 = (ProgressBar)findViewById(R.id.myProgressBar1);
mProgressBar01.setIndeterminate(false);
mButton01.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
mTextView01.setText(R.string.string_start);
mProgressBar01.setVisibility(View.VISIBLE);
mProgressBar01.setMax(100);
mProgressBar01.setProgress(0);
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<10;i++){
try{
Thread.sleep(1000);
intCounter = (i+1)*20;
if(i==5){
Message m = new Message();
m.what = MainActivity.GUI_STOP_NOTIFIER;
MainActivity.this.myMessageHandle.sendMessage(m);
break;
}else{
Message m = new Message();
m.what = MainActivity.GUI_THREADING_NOTIFIER;
MainActivity.this.myMessageHandle.sendMessage(m);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}).start();
}
});
}
Handler myMessageHandle = new Handler()
{
public void handleMessage(Message msg){
switch (msg.what){
case MainActivity.GUI_STOP_NOTIFIER:
mTextView01.setText(R.string.str_progress_done);
mProgressBar01.setVisibility(View.GONE);
Thread.currentThread().interrupt();
break;
case MainActivity.GUI_THREADING_NOTIFIER:
if(!Thread.currentThread().isInterrupted()){
mProgressBar01.setProgress(intCounter);
mTextView01.setText(getResources().getText(R.string.str_progress_start)+
"("+Integer.toString(intCounter)+"%)\n"+
"Progress:"+
Integer.toString(mProgressBar01.getProgress())+
"\n"+"Indeterminate:"+
Boolean.toString(mProgressBar01.isIndeterminate())
);
}
break;
}
super.handleMessage(msg);
}
};
}
2.main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:text= "@string/app_name"/>
<ProgressBar
android:id="@+id/myProgressBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<Button
android:id="@+id/myButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_send"
/>
</LinearLayout>
3.效果图
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。