alertDialog創(chuàng)建對話框
alertDialog生成對話框可以分為以下4個區(qū)域
1、圖標區(qū)
2卵酪、標題區(qū)
3、內(nèi)容區(qū)
4谤碳、按鈕區(qū)
public class MainActivity extends Activity {
? ? private ClipboardManager show;
? ? String[] items = new String[]{
? ? ? ? ? ? "列表項1",
? ? ? ? ? ? "列表項2",
? ? ? ? ? ? "列表項3",
? ? ? ? ? ? "列表項4"
? ? };
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.alertdialog);
? ? }
? ? public void simple(View source) {
? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(this)
? ? ? ? ? ? ? ? //設置對話框標題
? ? ? ? ? ? ? ? .setTitle("簡單對話框")
? ? ? ? ? ? ? ? //設置圖標
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
? ? ? ? ? ? ? ? .setMessage("對話框第二行測試內(nèi)容/n");
? ? ? ? //為alterDalog.builder添加“確定”按鈕
? ? ? ? setPositiveButton(builder);
? ? ? ? //為alterDalog.builer添加取消按鈕
? ? ? ? setNegativeButton(builder).create().show();
? ? }
? ? private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {
? ? ? ? //調(diào)用setpostivitybutton方法添加確定按鈕
? ? ? ? return builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? show.setText("單擊了【確定】按鈕");
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {
? ? ? ? return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? show.setText("單擊了【取消】按鈕");
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? public void simpleList(View source) {
? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(this)
? ? ? ? ? ? ? ? //設置對話框標題
? ? ? ? ? ? ? ? .setTitle("簡單列表項對話框")
? ? ? ? ? ? ? ? //設置圖標
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
? ? ? ? ? ? ? ? //設置簡單的列表項內(nèi)容
? ? ? ? ? ? ? ? .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.mia3)
? ? ? ? ? ? ? ? //設置單選列表項溃卡,默認選中第二項(索引為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.mia3)
? ? ? ? ? ? ? ? //設置多選列表項,設置勾選第二項蜒简,第四項
? ? ? ? ? ? ? ? .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.mia3)
? ? ? ? ? ? ? ? //設置自定義列表項
? ? ? ? ? ? ? ? .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) {
//? ? ? ? 加載界面布局文件
? ? ? ? TableLayout tl = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);
? ? ? ? new AlertDialog.Builder(this)
//? ? ? ? ? ? ? ? 設置對話框圖標
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
//? ? ? ? ? ? ? ? 設置對話框標題
? ? ? ? ? ? ? ? .setTitle("自定義對話框")
//? ? ? ? ? ? ? ? 設置對話框的view對象
? ? ? ? ? ? ? ? .setView(tl)
//? ? ? ? ? ? ? ? 為對話框設置一個確定按鈕
? ? ? ? ? ? ? ? .setPositiveButton("登錄", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? //此處可執(zhí)行登錄處理
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
//? ? 為對話框設置一個取消按鈕
? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? //取消登錄不進行任何操作
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
//創(chuàng)建并顯示對話框
? ? ? ? ? ? ? ? .create().show();
? ? }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical">
? ? <TextView
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="對話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="simple"
? ? ? ? android:text="消息對話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="simpleList"
? ? ? ? android:text="列表項對話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="singleChoice"
? ? ? ? android:text="單選列表對話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="multiChoice"
? ? ? ? android:text="多選列表對話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="customList"
? ? ? ? android:text="自定義列表對話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="customView"
? ? ? ? android:text="自定義view對話框"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:id="@+id/loginfrom">
? ? <TableRow>
? ? ? ? <TextView
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="用戶名"
? ? ? ? ? ? android:textSize="10pt" />
? ? ? ? <!--輸入用戶名文本框-->
? ? ? ? <EditText
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:hint="請?zhí)顚懙卿涃~號"
? ? ? ? ? ? android:selectAllOnFocus="true" />
? ? </TableRow>
? ? <TableRow>
? ? ? ? <TextView
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="密碼"
? ? ? ? ? ? android:textSize="10pt" />
? ? ? ? <!--輸入密碼的文本框-->
? ? ? ? <EditText
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:hint="請?zhí)顚懨艽a"
? ? ? ? ? ? android:password="true" />
? ? </TableRow>
? ? <TableRow>
? ? ? ? <TextView
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="電話號碼"
? ? ? ? ? ? android:textSize="10pt" />
? ? ? ? <!--輸入電話號碼的文本框-->
? ? ? ? <EditText
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:hint="請?zhí)顚懩拿艽a"
? ? ? ? ? ? android:selectAllOnFocus="true"
? ? ? ? ? ? android:phoneNumber="true" />
? ? </TableRow>
</TableLayout>