使用對話方塊 —— Dialog,使用對話方塊dialog
對話方塊就是一般的快顯視窗,主要用來提示使用者,和使用者互動。
建立Activity對話方塊
使用Activity類比對話方塊。這個比較簡單,主要是使用Activity內建的Dialog主題。
建立DialogActivity,並在AndroidManifest中註冊。
改變DialogActivity的主題:
<activity android:theme="@android:style/Theme.Dialog" android:name="com.whathecode.usingdialog.DialogActivity" android:label="@string/title_activity_dialog" ></activity>
DialogActivity程式碼範例:
package com.whathecode.usingdialog;import android.app.Activity;import android.os.Bundle;import android.view.View;public class DialogActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog); } //用來關閉這個Activity public void close(View view) { finish(); }}
DialogActivity布局檔案:
<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:padding="5dp" android:orientation="vertical" android:gravity="center_vertical|center_horizontal" tools:context=".DialogActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是基於Activity的Dialog" /> <LinearLayout android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確定" android:onClick="close"/> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" android:onClick="close"/> </LinearLayout></LinearLayout>
MainActivity代碼:
package com.whathecode.usingdialog;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.View;public class MainActivity extends FragmentActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void openActivityDialog(View view) { Intent intent = new Intent(this, DialogActivity.class); startActivity(intent); }}
運行效果:
package com.whathecode.usingdialog;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity{ private static final int SINGLE_CHOICE_DIALOG = 0; private static final int MULTI_CHOICE_DIALOG = 1; private static final int PROGRESS_DIALOG = 2; protected static final int MAX_PROGRESS = 30; private CharSequence items[] = new String[] { "apple", "google", "microsoft" }; private boolean checkedItems[] = new boolean[3]; private Handler progressHandler; private int progress; protected ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (progress >= MAX_PROGRESS) { progressDialog.dismiss(); //關閉progressDialog } else { progress++; //進度條加1 progressDialog.incrementProgressBy(1); //只要當前進度小於總進度,每個100毫秒發送一次訊息 progressHandler.sendEmptyMessageDelayed(0, 100); } } }; } public void openActivityDialog(View view) { Intent intent = new Intent(this, DialogActivity.class); startActivity(intent); } //顯示單選對話方塊 public void openSinglechoiceDialog(View view) { showDialog(SINGLE_CHOICE_DIALOG); } //顯示多選對話方塊 public void openMultichoiceDialog(View view) { showDialog(MULTI_CHOICE_DIALOG); } //顯示進度條對話方塊 public void openProgressDialog(View view) { showDialog(PROGRESS_DIALOG); progress = 0; progressDialog.setProgress(0); progressHandler.sendEmptyMessage(0); } @Override @Deprecated protected Dialog onCreateDialog(int id) { switch (id) { case SINGLE_CHOICE_DIALOG: return createSingleChoiceDialog(); case MULTI_CHOICE_DIALOG: return createMultichoiceDialog(); case PROGRESS_DIALOG: return createProgressDialog(); default: break; } return null; } /** * 建立單選對話方塊 * */ public Dialog createSingleChoiceDialog() { return new AlertDialog.Builder(this) .setTitle("單選對話方塊") //設定對話方塊標題 .setNegativeButton("取消", null) //設定取消按鈕鈕 .setPositiveButton("確定", null) //設定確定按 .setSingleChoiceItems(items, 0, //綁定資料 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(), items[which].toString(), Toast.LENGTH_SHORT).show(); } }).create(); } /** * 建立多選對話方塊 * */ public Dialog createMultichoiceDialog() { return new AlertDialog.Builder(this) .setTitle("多選對話方塊") //設定對話方塊標題 .setNegativeButton("取消", null) //設定取消按鈕 .setPositiveButton("確定", null) //設定確定按鈕 .setMultiChoiceItems(items, checkedItems, //綁定資料 new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText( getBaseContext(), isChecked ? items[which] + " check" : items[which] + " uncheck", Toast.LENGTH_SHORT).show(); } }).create(); } /** * 建立帶進度條的對話方塊 * */ public Dialog createProgressDialog() { progressDialog = new ProgressDialog(this); progressDialog.setTitle("下載對話方塊"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMax(MAX_PROGRESS); progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); return progressDialog; }}
運行效果:
這裡比較難理解還是ProgressDialog,因為它需要增加進度。這裡我們通過向Activity線程發送訊息,
從而能夠使用progressDialog.incrementProgressBy(1)方法遞增進度條。