編輯:關於 Android 開發
日常開發中常用的 Dialog 有確認框、單選框、多選框、列表框,以及自定義對話框。本篇透過一個完整範例,把它們一次講清楚。
先熟悉 AlertDialog 的常用方法:
setTitle 設置標題;setMessage 設置內容;setIcon 設置圖標;setItems 設置列表;setMultiChoiceItems 用於多選;setSingleChoiceItems 設置單選;setPositiveButton 添加確定按鈕;setNegativeButton 添加取消按鈕;setNeutralButton 添加普通按鈕;setView 設置自定義樣式。
先看運行效果:

MainActivity 的完整程式碼如下,五個按鈕分別觸發五種對話框:
public class MainActivity extends Activity implements OnClickListener {
private Button button1, button2, button3, button4, button5;
private int mark = 0;
private String item[] = { "學生", "工人", "教師", "農民" };
private String multChoice[] = { "旅游", "電影", "運動", "讀書" };
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btn_button1);
button2 = (Button) findViewById(R.id.btn_button2);
button3 = (Button) findViewById(R.id.btn_button3);
button4 = (Button) findViewById(R.id.btn_button4);
button5 = (Button) findViewById(R.id.btn_button5);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
}
@Override public void onClick(View v) {
switch (v.getId()) {
// 確認對話框
case R.id.btn_button1: {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("確認對話框");
builder.setMessage("確認退出?");
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你單擊了確定按鈕", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你單擊了取消按鈕", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
// 列表對話框
case R.id.btn_button2: {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("職業");
builder.setItems(item, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你的職業是" + item[which], Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
// 多選對話框
case R.id.btn_button3: {
final boolean choose[] = new boolean[multChoice.length];
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("愛好");
builder.setMultiChoiceItems(multChoice, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override public void onClick(DialogInterface dialog, int which, boolean isChecked) {
choose[which] = isChecked;
}
});
builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
String result = "";
for (int i = 0; i < multChoice.length; i++) {
if (choose[i]) { result += multChoice[i] + " "; }
}
Toast.makeText(MainActivity.this, "你的愛好[" + result + "]", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
// 單選對話框
case R.id.btn_button4: {
mark = 0;
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("職業");
builder.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
mark = which;
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你的職業:" + item[mark], Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
// 自定義對話框
case R.id.btn_button5: {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.item, null);
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("自定義對話框");
builder.setView(view);
builder.setNeutralButton("普通按鈕", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "我是自定義的對話框哦", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
}
}
}
主界面佈局很直觀,就是縱向排列的五個按鈕:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button android:id="@+id/btn_button1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="確認對話框" />
<Button android:id="@+id/btn_button2"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="列表對話框" />
<Button android:id="@+id/btn_button3"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="多選對話框" />
<Button android:id="@+id/btn_button4"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="單選對話框" />
<Button android:id="@+id/btn_button5"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="自定義對話框" />
</LinearLayout>
自定義對話框的佈局檔 item.xml 則放入自訂的圖片與文字:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/icon" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我是自定義的對話框" />
</LinearLayout>
掌握這些 AlertDialog 用法,再搭配 setView 換上自訂佈局,幾乎能滿足各種彈窗互動需求。希望對大家有幫助。
Android Bitmap 記憶體優化實戰
Android Bitmap 記憶體優化實戰 在 Android 中,圖片顯示是常見需求,但 Bitmap 也是記憶體消耗的大戶。若載入過大或未經最佳化的圖片,容易造成
手機安全衛士實作:電話號碼歸屬地查詢與震動提示
電話歸屬地查詢是高級工具中常見的小功能,使用者輸入號碼後,頁面能即時顯示號碼類型或地區。本文以 AddressActivity 與 AddressDao 為例,說明實作重
繞過系統分享面板:自己列舉可分享的 App 並完成 Intent 分享
繞過系統分享面板:自己列舉可分享的 App 並完成 Intent 分享 在 Android 做分享功能,網路上搜一下大概只有兩條路。第一條是直接丟一個 ACTION_SE
從 Android 5.1.1 源碼看應用進程的權限降級:Zygote fork 之後發生了什麼
前言 Android 上所有的應用進程,其實都是由 Zygote fork 出來的。而 Zygote 本身是以 root 權限運行的,這就帶來一個明顯的問題:新 fork