TestAsync.java
public class TestAsync extends Activity{
ProgressBar progressBar;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_handler);
progressBar=(ProgressBar)findViewById(R.id.progressBar);
textView=(TextView)findViewById(R.id.handler_text);
progressBar.setVisibility(View.INVISIBLE);
Button sendButton=(Button)findViewById(R.id.handler_sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyAsyncTask asyncTask=new MyAsyncTask();
asyncTask.execute("1000");
}
});
}
private class MyAsyncTask extends AsyncTask<String ,Integer,String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... strings) {
String result="";
int myProgress=0;
int inputLength=100;
for(int i=1;i<=inputLength;i++)
{
myProgress=i;
result=result+"*";
try{
Thread.sleep(100);
}catch (InterruptedException e){}
publishProgress(myProgress);
}
//返回一个值,将它返回给onPostExecute
return result;
}
@Override
protected void onProgressUpdate(Integer... values) {
//和UI线程同步,可以在这里对UII界面更新
//例如:更新进度条,Notification或者其他UI元素
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s) {
//和UI线程同步
//更行UI Dialog 或者Notifiaction报告结果
super.onPostExecute(s);
Toast.makeText(TestAsync.this,"Success",Toast.LENGTH_SHORT).show();
textView.setText(s);
progressBar.setVisibility(View.GONE);
}
}
}
activity_test_handle.xml
<LinearLayout 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:orientation="vertical"
>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_width="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:inputType="number"
android:hint="a:"
android:id="@+id/handler_parText1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<EditText
android:inputType="number"
android:hint="b:"
android:id="@+id/handler_parText2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/handler_sendButton"
android:text="send"
android:background="@drawable/send_button_selector"
android:layout_width="match_parent"
android:textColor="#ffffff"
android:textSize="30sp"
android:layout_height="wrap_content"/>
</TableRow>
<TextView
android:id="@+id/handler_text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="25sp"
/>
</LinearLayout>
版权声明:本文为博主原创文章,未经博主允许不得转载。