public class WorkerAsync extends AsyncTask<String, Void, Void> { // ProgressDialog private ProgressDialog mProgressDialog; public WorkerAsync(MainActivity activity) { mProgressDialog = new ProgressDialog(activity); mProgressDialog.setMessage("Working please wait....."); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); mProgressDialog.setCancelable(false); } protected void onPreExecute() { super.onPreExecute(); mProgressDialog.show(); } @Override protected Void doInBackground(String... par) { // Sleep try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } protected void onPostExecute(Void unused) { mProgressDialog.dismiss(); } }
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TableRow> <View android:layout_height="1dip" android:background="#CCCCCC" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.1" android:orientation="horizontal" > <Button android:id="@+id/btnShowProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Progress" /> </TableLayout> <View android:layout_height="1dip" android:background="#CCCCCC" /> <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="By.. ThaiCreate.Com" /> </LinearLayout> </TableLayout>
package com.myapp; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.view.View; import android.view.Menu; import android.widget.Button; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // *** Show Progress final Button btnShowProgress = (Button) findViewById(R.id.btnShowProgress); // Perform action on click btnShowProgress.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { new WorkerAsync(MainActivity.this).execute(); } }); } // Working Image in Background public class WorkerAsync extends AsyncTask<String, Void, Void> { // ProgressDialog private ProgressDialog mProgressDialog; public WorkerAsync(MainActivity activity) { mProgressDialog = new ProgressDialog(activity); mProgressDialog.setMessage("Working please wait....."); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); mProgressDialog.setCancelable(false); } protected void onPreExecute() { super.onPreExecute(); mProgressDialog.show(); } @Override protected Void doInBackground(String... par) { // Sleep try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } protected void onPostExecute(Void unused) { mProgressDialog.dismiss(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท