Android Control--7 forms of Android Dialog use Example (reprint)

Source: Internet
Author: User

In Android development, we often need to pop up some dialog boxes on the Android interface, such as asking the user or letting the user choose. These features we call it Android Dialog dialog, in the course of our use of Android, I summed up, the type of android dialog is nothing more than 7, I would like to introduce you to the following 7 Kinds of Android Dialog dialog box use method, I hope we can help you.

1. This effect is a prompt that pops up when you press the Back button to ensure error-correct operation, using a common dialog box style.

The method code for creating the Dialog dialog box is as follows:

12345678910111213141516171819 protectedvoiddialog() {    AlertDialog.Builder builder = newBuilder(Main.this);    builder.setMessage("确认退出吗?");    builder.setTitle("提示");    builder.setPositiveButton("确认"newOnClickListener() {     @Override     publicvoidonClick(DialogInterface dialog, intwhich) {      dialog.dismiss();      Main.this.finish();     }    });    builder.setNegativeButton("取消"newOnClickListener() {     @Override     publicvoidonClick(DialogInterface dialog, intwhich) {      dialog.dismiss();     }    });    builder.create().show();   }

This method is called in the onkeydown (int keycode, KeyEvent event) method

123456 publicboolean onKeyDown( int keyCode, KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ) {     dialog();    }    return false;   }

2. Changed the chart of the dialog box, added three buttons

The method code to create the dialog is as follows:

1234567891011121314151617181920212223242526 Dialog dialog = newAlertDialog.Builder(this).setIcon(       android.R.drawable.btn_star).setTitle("喜好调查").setMessage(       "你喜欢李连杰的电影吗?").setPositiveButton("很喜欢",       newOnClickListener() {        @Override        publicvoidonClick(DialogInterface dialog, intwhich) {         // TODO Auto-generated method stub         Toast.makeText(Main.this"我很喜欢他的电影。",           Toast.LENGTH_LONG).show();        }       }).setNegativeButton("不喜欢"newOnClickListener() {      @Override      publicvoidonClick(DialogInterface dialog, intwhich) {       // TODO Auto-generated method stub       Toast.makeText(Main.this"我不喜欢他的电影。" , Toast.LENGTH_LONG)         .show();      }     }).setNeutralButton("一般"newOnClickListener() {      @Override      publicvoidonClick(DialogInterface dialog, intwhich) {       // TODO Auto-generated method stub       Toast.makeText(Main.this"谈不上喜欢不喜欢。", Toast.LENGTH_LONG)         .show();      }     }).create();     dialog.show();

3. Information content is a simple view type

The code to create the dialog method is as follows:

1234 newAlertDialog.Builder( this ).setTitle( "请输入" ).setIcon(       android.R.drawable.ic_dialog_info).setView(       new EditText( this )).setPositiveButton( "确定" null )       .setNegativeButton( "取消" null).show();

4. Information content is a set of radio boxes

The code to create the dialog method is as follows:

1234 newAlertDialog.Builder( this ).setTitle( "复选框" ).setMultiChoiceItems(       new String[] { "Item1" "Item2" }, null null )       .setPositiveButton( "确定" null )       .setNegativeButton( "取消" null).show();

5. Information content is a set of multi-marquee

The code to create the dialog method is as follows:

12345678 newAlertDialog.Builder(this).setTitle("单选框").setIcon(       android.R.drawable.ic_dialog_info).setSingleChoiceItems(       newString[] { "Item1""Item2"}, 0,       newDialogInterface.OnClickListener() {        publicvoidonClick(DialogInterface dialog, intwhich) {         dialog.dismiss();        }       }).setNegativeButton("取消"null).show();

6. Information content is a set of simple list items

The method code to create the dialog is as follows:

123 newAlertDialog.Builder( this ).setTitle( "列表框" ).setItems(       new String[] { "Item1" "Item2" }, null ).setNegativeButton(       "确定" null).show();

7. Information content is a custom layout

Dialog layout file code is as follows:

123456789101112 <?xmlversion= "1.0"encoding="utf-8"?>  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"   android:layout_height="wrap_content"android:layout_width="wrap_content"   android:background ="#ffffffff"android:orientation="horizontal"   android:id="@+id/dialog">   <TextViewandroid:layout_height="wrap_content"     android:layout_width="wrap_content"    android:id="@+id/tvname"android:text="姓名:"/>   <EditTextandroid:layout_height="wrap_content"    android:layout_width="wrap_content"android:id= "@+id/etname"android:minWidth="100dip"/>  </LinearLayout>

The code to create the dialog method is as follows:

123456 LayoutInflater inflater = getLayoutInflater();     View layout = inflater.inflate(R.layout.dialog,       (ViewGroup) findViewById(R.id.dialog));     newAlertDialog.Builder( this ).setTitle( "自定义布局" ).setView(layout)       .setPositiveButton( "确定" null )       .setNegativeButton( "取消" null).show();

Android Control--7 forms of Android Dialog use Example (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.