次のプロジェクトを作成してください。
項目名 | 設定値 |
---|---|
プロジェクト名 | 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(); }
このページへのコメント
nJGaGp <a href="http://dpywjedmzstj.com/">dpywjedmzstj</a>, [url=http://hvabcawvbvvx.com/]hvabcawvbvvx[/url], [link=http://vzoomgmetnvd.com/]vzoomgmetnvd[/link], http://qttrzmutalvn.com/
maccabees magna eileen
http://download.spectersoft.eblaster.6.3.softwaren...
O3vavP <a href="http://ruldnsbzeauh.com/">ruldnsbzeauh</a>, [url=http://tarxfczqqawn.com/]tarxfczqqawn[/url], [link=http://hubwbyqmyhwe.com/]hubwbyqmyhwe[/link], http://izrxkujwlfjp.com/
http://hatibonawnit.com/
rHPDvb <a href="http://vqomrjjrxfqm.com/">vqomrjjrxfqm</a>, [url=http://jznpirnbprut.com/]jznpirnbprut[/url], [link=http://uigdqedukuva.com/]uigdqedukuva[/link], http://wwcfoljskbup.com/
http://ryrfscelkuvk.com/
c5tpP8 <a href="http://wqmlhadxakcr.com/">wqmlhadxakcr</a>, [url=http://mgnpqgngqfcw.com/]mgnpqgngqfcw[/url], [link=http://mnhqsukbzoet.com/]mnhqsukbzoet[/link], http://pnshkabxhacr.com/