編輯:關於 Android 程式設計
對話框是 Android 開發中出現頻率最高的元件之一。本文用一個小範例把五種常見形態走過一遍:普通提示、單選、多選、文字輸入、進度條。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/common_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:text="普通對話框"
android:textSize="16sp" />
<Button
android:id="@+id/radio_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:text="單選對話框"
android:textSize="16sp" />
<Button
android:id="@+id/check_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:text="多選對話框"
android:textSize="16sp" />
<Button
android:id="@+id/input_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:text="輸入文字對話框"
android:textSize="16sp" />
<Button
android:id="@+id/progress_dialog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:text="進度條對話框"
android:textSize="16sp" />
</LinearLayout>
輸入型對話框需要一個自訂版面 activity_input.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帳號" />
<EditText
android:id="@+id/uname"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密碼" />
<EditText
android:id="@+id/upass"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends Activity implements OnClickListener {
private boolean[] checked = { true, true, true, false };
private int progressNumber;
private ProgressDialog progressDialog;
private Button commonBtn, radioBtn, checkBtn, inputBtn, progressBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
}
private void initViews() {
this.commonBtn = (Button) findViewById(R.id.common_dialog);
this.radioBtn = (Button) findViewById(R.id.radio_dialog);
this.checkBtn = (Button) findViewById(R.id.check_dialog);
this.inputBtn = (Button) findViewById(R.id.input_dialog);
this.progressBtn = (Button) findViewById(R.id.progress_dialog);
}
private void initListeners() {
this.commonBtn.setOnClickListener(this);
this.radioBtn.setOnClickListener(this);
this.checkBtn.setOnClickListener(this);
this.inputBtn.setOnClickListener(this);
this.progressBtn.setOnClickListener(this);
}
/** 1. 普通對話框 */
private Dialog buildAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話框");
builder.setMessage("您的密碼不對!!");
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.mm1);
builder.setView(imageView);
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是左邊確定按鈕!");
}
});
builder.setNeutralButton("詳情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是中間詳情按鈕!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是右邊取消按鈕!");
}
});
return builder.create();
}
/** 2. 單選對話框,第二參數為預設勾選的索引 */
private Dialog buildAlertDialog_radio() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話框");
builder.setSingleChoiceItems(
new String[]{"大學", "高中", "初中", "小學"}, 1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: setTitle("大學"); break;
case 1: setTitle("高中"); break;
case 2: setTitle("初中"); break;
default: setTitle("小學"); break;
}
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是左邊確定按鈕!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是右邊取消按鈕!");
}
});
return builder.create();
}
/** 3. 多選對話框 */
private Dialog buildAlertDialog_checkbox() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話框");
builder.setMultiChoiceItems(
new String[]{"大學", "高中", "初中", "小學"}, checked,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
setTitle("which=" + which + "-----" + "isChecked=" + isChecked);
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊了確定按鈕!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是了取消按鈕!");
}
});
return builder.create();
}
/** 4. 含文字輸入的對話框 */
private Dialog buildAlertDialog_input() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("對話框");
LayoutInflater inflater = LayoutInflater.from(this);
builder.setView(inflater.inflate(R.layout.activity_input, null));
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是確定按鈕!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setTitle("您點擊的是取消按鈕!");
}
});
return builder.create();
}
/** 5. 進度條對話框 */
private Dialog buildAlertDialog_progress() {
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("進度條");
progressDialog.setMessage("正在下載...........");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setIndeterminate(false);
return progressDialog;
}
/** 每隔 0.3 秒更新一次進度 */
public void updateProgress() {
new Thread() {
@Override
public void run() {
try {
while (progressNumber <= 100) {
progressDialog.setProgress(progressNumber++);
Thread.sleep(300);
}
progressDialog.cancel();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.common_dialog:
buildAlertDialog().show();
break;
case R.id.radio_dialog:
buildAlertDialog_radio().show();
break;
case R.id.check_dialog:
buildAlertDialog_checkbox().show();
break;
case R.id.input_dialog:
buildAlertDialog_input().show();
break;
case R.id.progress_dialog:
buildAlertDialog_progress().show();
updateProgress();
break;
default:
break;
}
}
}
setMessage 與 setView 互斥。兩者都會佔用內容區,同時設定只有一個會生效。需要圖文並用時,請把 TextView 一起放進自訂版面。
setItems、setSingleChoiceItems、setMultiChoiceItems 三者也互斥,一次只能用一個。
單選對話框的 setSingleChoiceItems 第二個參數是預設選取的索引,-1 代表不預選。這個值很容易寫錯,導致開起來沒勾選。
範例中點選單選項時呼叫了 setTitle(),這是為了展示 which 的值,實務上比較常見的做法是在按確定時才一次取出結果。若要這樣做,記得把選中的索引存到成員變數,因為 onClick 回呼裡拿不到最後的選擇。
最後要特別提醒:ProgressDialog 在 API 26 已被標記為 deprecated。原因是它會阻擋使用者操作,在現代 Android 的設計指引中不被鼓勵。新專案若需要表達進行中的狀態,請改用 SwipeRefreshLayout、 indeterminate 的 ProgressBar,或直接在通知欄顯示下載進度。這段程式碼仍有參考價值,但正式產品中應避免沿用。
另外,updateProgress 直接在背景執行緒呼叫 setProgress 是不安全的寫法。UI 更新必須回到主執行緒,正確做法是透過 Handler、runOnUiThread 或 AsyncTask 的 onProgressUpdate。範例之所以能跑,是因為 ProgressDialog 內部部分方法容忍跨執行緒呼叫,但這不該被當成依據。
Android Studio 自訂 EditText 點擊圖示一鍵清空文字
這篇文章是在「自訂 EditText 樣式」之後的功能強化,對實際專案有不小的參考價值。有興趣的朋友可以搭配上一篇〈Android Studio 自訂 EditText
Android Studio 快捷鍵自訂與自動導包設定完整教學
剛從 Eclipse 轉到 Android Studio 的那段時間,最不習慣的其實不是 Gradle,而是鍵盤。手指記了七八年的 Eclipse 快捷鍵,換到 AS 之
Android ListView 分類清單的簡易實作
在許多應用中,例如城市選擇、商品分類或設定分組,常會需要讓 ListView 顯示類似「分類標題 + 子項目」的結構。相比嵌套多個 ListView 或使用複雜控制項,一
Android 設計模式:工廠方法模式與簡單工廠
工廠方法模式是什麼?工廠方法模式是一種建立型設計模式。它的核心概念是:定義一個建立物件的介面,讓子類決定要實例化哪一個類別。這樣可以讓物件的建立延遲到子類,讓系統更容易擴