Several different Android Dialog

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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 protected void dialog() {     AlertDialog.Builder builder = new Builder(Main. this );     builder.setMessage( "确认退出吗?" );     builder.setTitle( "提示" );     builder.setPositiveButton( "确认" , new OnClickListener() {      @Override      public void onClick(DialogInterface dialog, int which) {       dialog.dismiss();       Main. this .finish();      }     });     builder.setNegativeButton( "取消" , new OnClickListener() {      @Override      public void onClick(DialogInterface dialog, int which) {       dialog.dismiss();      }     });     builder.create().show();    }

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

?

1 2 3 4 5 6 public boolean 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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Dialog dialog = new AlertDialog.Builder( this ).setIcon(        android.R.drawable.btn_star).setTitle( "喜好调查" ).setMessage(        "你喜欢李连杰的电影吗?" ).setPositiveButton( "很喜欢" ,        new OnClickListener() {         @Override         public void onClick(DialogInterface dialog, int which) {          // TODO Auto-generated method stub          Toast.makeText(Main. this , "我很喜欢他的电影。" ,            Toast.LENGTH_LONG).show();         }        }).setNegativeButton( "不喜欢" , new OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {        // TODO Auto-generated method stub        Toast.makeText(Main. this , "我不喜欢他的电影。" , Toast.LENGTH_LONG)          .show();       }      }).setNeutralButton( "一般" , new OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {        // 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:

?

1 2 3 4 new AlertDialog.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:

?

1 2 3 4 new AlertDialog.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:

?

1 2 3 4 5 6 7 8 New alertdialog.builder ( this ). Settitle ( "Radio Box" ). SetIcon (      android. R.drawable.ic_dialog_info). Setsinglechoiceitems (      New string[] { "Item1" , "Item2" }, 0 ,      new Dialoginterface.onclicklistener () {       public void OnClick (dialoginterface dialog, int which) {   & nbsp;    Dialog.dismiss ();      }     }). Setnegativebutton ( code> "Cancel" , null ). Show ();

6. Information content is a set of simple list items

The method code to create the dialog is as follows:

?

1 2 3 new AlertDialog.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:

?

1 2 3 4 5 6 7 8 9 10 11 12 <? xml version = "1.0" encoding = "utf-8" ?>    < LinearLayout xmlns: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" >    < TextView android:layout_height = "wrap_content"      android:layout_width = "wrap_content"     android:id = "@+id/tvname" android:text = "姓名:" />    < EditText android: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:

?

1 2 3 4 5 6 LayoutInflater inflater = getLayoutInflater();      View layout = inflater.inflate(R.layout.dialog,        (ViewGroup) findViewById(R.id.dialog));      new AlertDialog.Builder( this ).setTitle( "自定义布局" ).setView(layout)        .setPositiveButton( "确定" , null )        .setNegativeButton( "取消" , null ).show();

OK, the above 7 Kinds of Android Dialog dialog box use method introduced here, basically all, if everyone in the Android development process encountered dialog time can take out to see.

Also note that this article refers to the article:
Http://android.tgbus.com/Android/tutorial/201107/359812.shtml

Several different Android Dialog

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.