alertDialog創(chuàng)建對(duì)話框
alertDialog生成對(duì)話框可以分為以下4個(gè)區(qū)域
1、圖標(biāo)區(qū)
2、標(biāo)題區(qū)
3、內(nèi)容區(qū)
4、按鈕區(qū)
public class MainActivity extends Activity {
? ? private ClipboardManager show;
? ? String[] items = new String[]{
? ? ? ? ? ? "列表項(xiàng)1",
? ? ? ? ? ? "列表項(xiàng)2",
? ? ? ? ? ? "列表項(xiàng)3",
? ? ? ? ? ? "列表項(xiàng)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)
? ? ? ? ? ? ? ? //設(shè)置對(duì)話框標(biāo)題
? ? ? ? ? ? ? ? .setTitle("簡(jiǎn)單對(duì)話框")
? ? ? ? ? ? ? ? //設(shè)置圖標(biāo)
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
? ? ? ? ? ? ? ? .setMessage("對(duì)話框第二行測(cè)試內(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)
? ? ? ? ? ? ? ? //設(shè)置對(duì)話框標(biāo)題
? ? ? ? ? ? ? ? .setTitle("簡(jiǎn)單列表項(xiàng)對(duì)話框")
? ? ? ? ? ? ? ? //設(shè)置圖標(biāo)
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
? ? ? ? ? ? ? ? //設(shè)置簡(jiǎn)單的列表項(xiàng)內(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)
? ? ? ? ? ? ? ? //設(shè)置對(duì)話框標(biāo)題
? ? ? ? ? ? ? ? .setTitle("單選列表項(xiàng)對(duì)話框")
? ? ? ? ? ? ? ? //設(shè)置圖標(biāo)
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
? ? ? ? ? ? ? ? //設(shè)置單選列表項(xiàng),默認(rèn)選中第二項(xiàng)(索引為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)
? ? ? ? ? ? ? ? //設(shè)置對(duì)話框標(biāo)題
? ? ? ? ? ? ? ? .setTitle("多選列表項(xiàng)對(duì)話框")
? ? ? ? ? ? ? ? //設(shè)置圖標(biāo)
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
? ? ? ? ? ? ? ? //設(shè)置多選列表項(xiàng),設(shè)置勾選第二項(xiàng),第四項(xiàng)
? ? ? ? ? ? ? ? .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)
? ? ? ? ? ? ? ? //設(shè)置對(duì)話框標(biāo)題
? ? ? ? ? ? ? ? .setTitle("自定義列表對(duì)話框")
? ? ? ? ? ? ? ? //設(shè)置圖標(biāo)
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
? ? ? ? ? ? ? ? //設(shè)置自定義列表項(xiàng)
? ? ? ? ? ? ? ? .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)
//? ? ? ? ? ? ? ? 設(shè)置對(duì)話框圖標(biāo)
? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)
//? ? ? ? ? ? ? ? 設(shè)置對(duì)話框標(biāo)題
? ? ? ? ? ? ? ? .setTitle("自定義對(duì)話框")
//? ? ? ? ? ? ? ? 設(shè)置對(duì)話框的view對(duì)象
? ? ? ? ? ? ? ? .setView(tl)
//? ? ? ? ? ? ? ? 為對(duì)話框設(shè)置一個(gè)確定按鈕
? ? ? ? ? ? ? ? .setPositiveButton("登錄", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? //此處可執(zhí)行登錄處理
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
//? ? 為對(duì)話框設(shè)置一個(gè)取消按鈕
? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? //取消登錄不進(jìn)行任何操作
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
//創(chuàng)建并顯示對(duì)話框
? ? ? ? ? ? ? ? .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="對(duì)話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="simple"
? ? ? ? android:text="消息對(duì)話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="simpleList"
? ? ? ? android:text="列表項(xiàng)對(duì)話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="singleChoice"
? ? ? ? android:text="單選列表對(duì)話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="multiChoice"
? ? ? ? android:text="多選列表對(duì)話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="customList"
? ? ? ? android:text="自定義列表對(duì)話框"/>
? ? <Button
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="customView"
? ? ? ? android:text="自定義view對(duì)話框"/>
</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="請(qǐng)?zhí)顚懙卿涃~號(hào)"
? ? ? ? ? ? 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="請(qǐng)?zhí)顚懨艽a"
? ? ? ? ? ? android:password="true" />
? ? </TableRow>
? ? <TableRow>
? ? ? ? <TextView
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="電話號(hào)碼"
? ? ? ? ? ? android:textSize="10pt" />
? ? ? ? <!--輸入電話號(hào)碼的文本框-->
? ? ? ? <EditText
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:hint="請(qǐng)?zhí)顚懩拿艽a"
? ? ? ? ? ? android:selectAllOnFocus="true"
? ? ? ? ? ? android:phoneNumber="true" />
? ? </TableRow>
</TableLayout>
