AlertDialog能夠生成各類內容的對話框,它生成的對話框包含4個區域:
圖標區,標題區,內容區,按鈕區android
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="allegro.alertdialog.MainActivity" tools:layout_editor_absoluteY="81dp" tools:layout_editor_absoluteX="0dp"> <!-- 顯示一個普通的文本編輯框組件 --> <EditText android:id="@+id/show" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:editable="false" android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" tools:layout_constraintRight_creator="1" tools:layout_constraintLeft_creator="1" /> <!-- 定義一個普通的按鈕組件 --> <Button android:layout_width="88dp" android:layout_height="wrap_content" android:text="簡單對話框" android:onClick="simple" android:id="@+id/button" android:layout_marginEnd="26dp" app:layout_constraintRight_toLeftOf="@+id/button2" tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1" tools:layout_constraintBottom_creator="1" app:layout_constraintBottom_toTopOf="@+id/button2" app:layout_constraintTop_toTopOf="@+id/button2" /> <!-- 定義一個普通的按鈕組件 --> <Button android:layout_width="88dp" android:layout_height="wrap_content" android:text="簡單列表項對話框" android:onClick="simpleList" android:id="@+id/button4" android:layout_marginStart="16dp" tools:layout_constraintTop_creator="1" android:layout_marginTop="52dp" app:layout_constraintTop_toBottomOf="@+id/button3" tools:layout_constraintLeft_creator="1" app:layout_constraintLeft_toLeftOf="@+id/button3" /> <!-- 定義一個普通的按鈕組件 --> <Button android:layout_width="88dp" android:layout_height="wrap_content" android:text="單選列表項對話框" android:onClick="singleChoice" android:layout_marginEnd="36dp" tools:layout_constraintRight_creator="1" tools:layout_constraintBottom_creator="1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginBottom="191dp" /> <!-- 定義一個普通的按鈕組件 --> <Button android:layout_width="88dp" android:layout_height="wrap_content" android:text="多選列表項對話框" android:onClick="multiChoice" tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1" tools:layout_constraintBottom_creator="1" app:layout_constraintBottom_toTopOf="@+id/button2" android:layout_marginEnd="16dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="@+id/button2" /> <!-- 定義一個普通的按鈕組件 --> <Button android:layout_width="88dp" android:layout_height="wrap_content" android:text="自定義列表項對話框" android:onClick="customList" android:id="@+id/button2" android:layout_marginStart="6dp" tools:layout_constraintBottom_creator="1" app:layout_constraintBottom_toTopOf="@+id/button3" tools:layout_constraintLeft_creator="1" android:layout_marginBottom="9dp" app:layout_constraintLeft_toRightOf="@+id/button3" /> <!-- 定義一個普通的按鈕組件 --> <Button android:layout_width="88dp" android:layout_height="wrap_content" android:text="自定義View對話框" android:onClick="customView" android:id="@+id/button3" android:layout_marginStart="78dp" tools:layout_constraintTop_creator="1" android:layout_marginTop="186dp" tools:layout_constraintLeft_creator="1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
package allegro.alertdialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.TableLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView show; String[] items = new String[] { "瘋狂Java講義", "瘋狂Ajax講義", "輕量級Java EE企業應用實戰", "瘋狂Android講義" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show = (TextView) findViewById(R.id.show); } public void simple(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設置對話框標題 .setTitle("簡單對話框") // 設置圖標 .setIcon(R.drawable.tools) .setMessage("對話框的測試內容\n第二行內容"); // 爲AlertDialog.Builder添加「肯定」按鈕 setPositiveButton(builder); // 爲AlertDialog.Builder添加「取消」按鈕 setNegativeButton(builder) .create() .show(); } public void simpleList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設置對話框標題 .setTitle("簡單列表對話框") // 設置圖標 .setIcon(R.drawable.tools) // 設置簡單的列表項內容 .setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你選中了《" + items[which] + "》"); } }); // 爲AlertDialog.Builder添加「肯定」按鈕 setPositiveButton(builder); // 爲AlertDialog.Builder添加「取消」按鈕 setNegativeButton(builder) .create() .show(); } public void singleChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設置對話框標題 .setTitle("單選列表項對話框") // 設置圖標 .setIcon(R.drawable.tools) // 設置單選列表項,默認選中第二項(索引爲1) .setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你選中了《" + items[which] + "》"); } }); // 爲AlertDialog.Builder添加「肯定」按鈕 setPositiveButton(builder); // 爲AlertDialog.Builder添加「取消」按鈕 setNegativeButton(builder) .create() .show(); } public void multiChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設置對話框標題 .setTitle("多選列表項對話框") // 設置圖標 .setIcon(R.drawable.tools) // 設置多選列表項,設置勾選第2項、第4項 .setMultiChoiceItems(items , new boolean[]{false , true ,false ,true}, null); // 爲AlertDialog.Builder添加「肯定」按鈕 setPositiveButton(builder); // 爲AlertDialog.Builder添加「取消」按鈕 setNegativeButton(builder) .create() .show(); } public void customList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設置對話框標題 .setTitle("自定義列表項對話框") // 設置圖標 .setIcon(R.drawable.tools) // 設置自定義列表項 .setAdapter(new ArrayAdapter<String>(this , R.layout.array_item , items), null); // 爲AlertDialog.Builder添加「肯定」按鈕 setPositiveButton(builder); // 爲AlertDialog.Builder添加「取消」按鈕 setNegativeButton(builder) .create() .show(); } public void customView(View source) { // 裝載app\src\main\res\layout\login.xml界面佈局文件 TableLayout loginForm = (TableLayout)getLayoutInflater() .inflate( R.layout.login, null); new AlertDialog.Builder(this) // 設置對話框的圖標 .setIcon(R.drawable.tools) // 設置對話框的標題 .setTitle("自定義View對話框") // 設置對話框顯示的View對象 .setView(loginForm) // 爲對話框設置一個「肯定」按鈕 .setPositiveButton("登陸", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 此處可執行登陸處理 } }) // 爲對話框設置一個「取消」按鈕 .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消登陸,不作任何事情 } }) // 建立並顯示對話框 .create() .show(); } private AlertDialog.Builder setPositiveButton( AlertDialog.Builder builder) { // 調用setPositiveButton方法添加「肯定」按鈕 return builder.setPositiveButton("肯定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("單擊了【肯定】按鈕!"); } }); } private AlertDialog.Builder setNegativeButton( AlertDialog.Builder builder) { // 調用setNegativeButton方法添加「取消」按鈕 return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("單擊了【取消】按鈕!"); } }); } }