ºÇ½ª¹¹¿·¡§
t_yokky2002 2011ǯ11·î24Æü(ÌÚ) 03:46:00ÍúÎò
¼¡¤Î¥×¥í¥¸¥§¥¯¥È¤òºîÀ®¤·¤Æ¤¯¤À¤µ¤¤¡£
¹àÌÜ̾ | ÀßÄêÃÍ |
---|---|
¥×¥í¥¸¥§¥¯¥È̾ | 12th_practice_1 |
¥Ó¥ë¥É¥¿¡¼¥²¥Ã¥È | Android 2.2 |
¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾ | ¥×¥í¥°¥ì¥¹¥Ð¡¼ |
¥Ñ¥Ã¥±¡¼¥¸Ì¾ | jp.co.example.practice |
¥¢¥¯¥Æ¥£¥Ó¥Æ¥£¤ÎºîÀ® | MainActivity |
ºÇ¾®SDK¥Ð¡¼¥¸¥ç¥ó | 8 |
res/layout/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="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:text="Toast¤òɽ¼¨" /> <Button android:id="@+id/buttonDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dip" android:text="Dialog¤òɽ¼¨" /> <ProgressBar android:id="@+id/ProgressBarSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dip" style="?android:attr/progressBarStyleSmall" /> <ProgressBar android:id="@+id/ProgressBarNormal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dip" style="?android:attr/progressBarStyle" /> <ProgressBar android:id="@+id/ProgressBarLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dip" style="?android:attr/progressBarStyleLarge" /> <ProgressBar android:id="@+id/ProgressBarHorizonInd" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dip" style="?android:attr/progressBarStyleHorizontal" /> <ProgressBar android:id="@+id/ProgressBarHorizon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dip" style="?android:attr/progressBarStyleHorizontal" /> </LinearLayout>
![](https://image02.seesaawiki.jp/a/r/androider/d4f29765917bba84.png)
src/jp.co.example.practice/MainActivity.javan¤ÎonCreate()¥á¥½¥Ã¥É¤ÎÆâÍƤò¡¢Êѹ¹¤·¤Æ¤¯¤À¤µ¤¤¡£
@Override public class MainActivity extends Activity { private ProgressBar progressbarHorizon; private ProgressBar progressbarHorizonInd; private Button button; private Button buttonDialog; private int progressStatus; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progressbar); progressbarHorizon = (ProgressBar) findViewById(R.id.ProgressBarHorizon); progressbarHorizonInd = (ProgressBar) findViewById(R.id.ProgressBarHorizonInd); progressbarHorizonInd.setIndeterminate(true); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ProgressBarTaskActivity.this, "¥È¡¼¥¹¥È¤òɽ¼¨", Toast.LENGTH_SHORT).show(); } }); buttonDialog = (Button) findViewById(R.id.buttonDialog); buttonDialog.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(ProgressBarTaskActivity.this) .setTitle("AlertDialog!") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } }); }
src/jp.co.example.practice/MainActivity.javan¤Ë¡¢MyAsyncTask¥¯¥é¥¹¤òÄɲ䷤Ƥ¯¤À¤µ¤¤¡£
class MyAsyncTask extends AsyncTask<Void, Integer, Void> { final String TAG = "MyAsyncTask"; @Override protected void onPreExecute() { Log.d(TAG, "onPreExecute"); progressStatus = 0; } @Override protected Void doInBackground(Void... params) { Log.d(TAG, "doInBackground"); while (true) { if (progressStatus >= 100) { progressStatus = 0; } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } // ¥×¥í¥°¥ì¥¹¥¹¥Æ¡¼¥¿¥¹¤Ë°ìÄêÃͤò²Ã»» progressStatus += 5; // ¥×¥í¥°¥ì¥¹¥¹¥Æ¡¼¥¿¥¹¤òÈ¿±Ç publishProgress(progressStatus); } } @Override protected void onProgressUpdate(Integer... values) { Log.d(TAG, "onProgressUpdate - " + values[0]); progressbarHorizon.setProgress(values[0]); } @Override protected void onPostExecute(Void result) { Log.d(TAG, "onPostExecute - " + result); } }
src/jp.co.example.practice/MainActivity.javan¤Ë¡¢MyAsyncTask¤Î¼Â¹Ô½èÍý¤òÄɲ䷤Ƥ¯¤À¤µ¤¤¡£
public void onCreate(Bundle savedInstanceState) { // ------ Ãæά ------ buttonDialog.setOnClickListener(new OnClickListener() { ¡¡¡¡¡¡¡¡¡¡¡¡// ------ Ãæά ------ }); // ¤³¤Î½èÍý¤òÄɲà new MyAsyncTask().execute(); }
¥³¥á¥ó¥È¤ò¤«¤¯