Step1

次のプロジェクトを作成してください。
項目名設定値
プロジェクト名12th_practice_1
ビルドターゲットAndroid 2.2
アプリケーション名プログレスバー
パッケージ名jp.co.example.practice
アクティビティの作成MainActivity
最小SDKバージョン8

Step2

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>

Step3

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();
		}
	});
}

Step4

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);
	}
}

Step5

src/jp.co.example.practice/MainActivity.javanに、MyAsyncTaskの実行処理を追加してください。
public void onCreate(Bundle savedInstanceState) {
	
	// ------ 中略  ------

	buttonDialog.setOnClickListener(new OnClickListener() {
      // ------ 中略  ------
	});
	
	// この処理を追加
	new MyAsyncTask().execute();
}

Step6

実行し、以下のように表示されましたか?
「トーストを表示」・「Dialogを表示」ボタンを押下しても、プログレスバーが更新され続けることを認してください。

このページへのコメント

nJGaGp <a href="http://dpywjedmzstj.com/">dpywjedmzstj</a>, [url=http://hvabcawvbvvx.com/]hvabcawvbvvx[/url], [link=http://vzoomgmetnvd.com/]vzoomgmetnvd[/link], http://qttrzmutalvn.com/

0
Posted by cupdkwrswlo 2013年11月14日(木) 10:56:08 返信

maccabees magna eileen

0
Posted by seen 2013年10月03日(木) 06:41:40
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/

0
Posted by qkokcjpup 2013年09月22日(日) 03:54:41
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/

0
Posted by iocpudtm 2013年07月07日(日) 01:37:06
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/

0
Posted by hjvyjcyc 2013年07月06日(土) 09:07:09 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

管理人/副管理人のみ編集できます