"Android" a variety of pop-up boxes with the menu key, the return key monitoring

Source: Internet
Author: User

Android comes with a variety of pop-up boxes, and the popup is one of the basic components of Android. At the same time, the Android program can monitor the menu keys, return keys, However, after Android 4.0, it is forbidden to block and listen to the home button, which is forced to remain the system daemon button, if the home key is not screened and listened to, it will appear java.lang.IllegalArgumentException:Window type can not was changed after the window was added. Error.

Below is a small program to illustrate the various Android pop-up boxes, while the Android is how to monitor the menu keys, return keys.

Such as:


Press the menu key to eject the message,

This program then provides a variety of popup boxes.

Each popup box adds a different listener that listens to the user's clicks on individual buttons.

Finally, press the Back button to end the program.

This program is relatively simple, just one activity.

1, the first is in the Res\values\strings.xml set the app name and the individual button display content as follows:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string    name= "app_name" > Pop-up box </string >    <string name= "action_settings" >Settings</string>    <string name= "Button1" > Pop-up box with multiple buttons </string>    <string name= "Button2" > Pop-up box with list </string>    <string name= "Button3" > Pop-up box with a radio list </string>    <string name= "Button4" > Pop-up box with multiple selection list </string></resources>
2, after, and "Android" using notification operating equipment notification bar (click the Open link), in Res\layout\activity_main.xml set a top-down linear layout. Place four buttons, give four buttons, set different IDs, and listen to Mainactivity.java for a while. A variety of pop-up boxes are generated from Java.

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent"         android:layout_height= "match_parent" android:orientation= "vertical" > <button android:id= "@+id/button1" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/butto N1 "android:textsize=" 24sp "/> <button android:id=" @+id/button2 "android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "android:text=" @string/button2 "android:textsize=" 24SP " /> <button android:id= "@+id/button3" android:layout_width= "Wrap_content" Android:layout_heig ht= "Wrap_content" android:text= "@string/button3" android:textsize= "24sp"/> <button android: Id= "@+id/button4" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:te        xt= "@string/button4"Android:textsize= "24SP"/></linearlayout> 

3. Finally, the Mainactivity.java file is written. The whole program is divided into two parts, one is to get the various buttons after the Click event on different buttons, add a variety of click events, which generated a different pop-up box. Pop-up box with multiple buttons via alertdialog alertdialog = new Alertdialog.builder (mainactivity.this). Create (); a dialog box with a list, is created by alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); They can all be set by SetIcon and Settitle with the title, but after the listener's title is different. Note the name of the listener, the name of the listener, must be declared in its own class to differentiate, otherwise it cannot be compiled.

The other part of the program is the monitoring of the physical button, which is very simple. You do not need to use XML to declare, write the listening code directly.

Package Com.alertdialog;import Android.os.bundle;import Android.view.keyevent;import android.view.view;import Android.widget.button;import Android.widget.toast;import Android.app.activity;import Android.app.AlertDialog; Import Android.content.dialoginterface;import Android.content.dialoginterface.onmultichoiceclicklistener;public Class Mainactivity extends Activity {private button button1;private button button2;private button button3;private button b Utton4;private string[] ListItems = new string[] {"Option 1", "Option 2", "option 3", "Option 4"};//option list item array @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) Findviewbyid ( R.id.button1) button2 = (Button) Findviewbyid (r.id.button2); button3 = (Button) Findviewbyid (r.id.button3); button4 = ( button) Findviewbyid (r.id.button4);//Here the normal button click Listener is different from the dialog button's click Listener, to declare is the view class Button1.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0){Alertdialog Alertdialog = new Alertdialog.builder (mainactivity.this). Create (); Alertdialog.seticon (R.drawable.ic_ launcher);//The Icon for the settings dialog is the application's icon Alertdialog.settitle ("Popup with Multiple Buttons"), Alertdialog.setmessage ("Contents of the dialog box");// Here the dialog button click Listener is different from the normal button click Listener, to declare is Dialoginterface this class Alertdialog.setbutton (Dialoginterface.button_negative, "Cancel", New Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {//TODO Auto-generated method Stubtoast.maketext (Mainactivity.this, "Cancel button is clicked", Toast.length_long). Show ();}); /Do not want this button do not write this method Alertdialog.setbutton (dialoginterface.button_neutral, "neutral", new Dialoginterface.onclicklistener () { @Overridepublic void OnClick (dialoginterface arg0, int arg1) {//TODO auto-generated method Stubtoast.maketext ( Mainactivity.this, "neutral button is clicked", Toast.length_long). Show ();}); /Do not want this button do not write this method Alertdialog.setbutton (dialoginterface.button_positive, "OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {//TODO auto-generated Method Stubtoast.maketext (mainactivity.this, "OK button is clicked", Toast.length_long). Show ();}); Alertdialog.show ();//must have this method, otherwise the dialog box does not show}}); Button2.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubalertdialog.builder Builder = new Alertdialog.builder (mainacti Vity.this);//The builder that distinguishes it from other components must be written Builder.seticon (r.drawable.ic_launcher);//The Icon for the Settings dialog box is the icon for the application Builder.settitle ("Popup with List"); Builder.setitems (Listitems,new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface arg0, int which) {Toast.maketext (Mainactivity.this,listitems[which] + "clicked", Toast.length_long). Show ( );}}); Builder.create (). Show ();//must have this method, otherwise the dialog box does not show}}); Button3.setonclicklistener (new View.onclicklistener () {Private String chooseitem;//is used to record the selected item @overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubalertdialog.builder builder = new Alertdialog.builder (mainactivity.this);//The builder that distinguishes it from other components must be written like this builder. SetIcon (R.drawable.ic_launcher);//The Icon for the Settings dialog box is the application's icon Builder.settitle ("Popup with List"); Builder.setsinglechoiceitems ( ListItems, 0,new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int which) { Chooseitem = Listitems[which];}); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int which) {Toast.maketext (Mainactivity.this,chooseitem + "clicked", Toast.length_long). Show ();}); Builder.create (). Show ();//must have this method, otherwise the dialog box does not show}}); Button4.setonclicklistener (new View.onclicklistener () {Private Boolean[] checkeditems;//used to record the selected item @overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubalertdialog.builder builder = new Alertdialog.builder (mainactivity.this);//The builder that distinguishes it from other components, This must be written Builder.seticon (r.drawable.ic_launcher);//The Icon for the settings dialog is the application's icon Builder.settitle ("Popup with List"); CheckedItems = new Boolean[] {false, False, True, True};//initializes the selected item Builder.setmultichoiceitems (ListItems, CheckeditemS,new Onmultichoiceclicklistener () {//Multiple selection listeners are independent, so there is no need to declare their class @overridepublic void OnClick (Dialoginterface arg0, int which, Boolean isChecked) {//TODO auto-generated method Stubcheckeditems[which] = ischecked;//Any item is selected or not, its corresponding array of Boolean values are Change}); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface arg0, int which) {//Find the option that corresponds to the selected item string result = ""; for (int i = 0; i < checkeditems.length; i++) {if (Checkeditems[i]) {result + = Listitems[i] + ",";}} if (!result.equals ("")) {///If the user has a choice toast.maketext (Mainactivity.this,result + "is selected", Toast.length_long). Show (); else {toast.maketext (mainactivity.this, "no option is selected", Toast.length_long). Show ();}}); Builder.create (). Show ();//must have this method, otherwise the dialog box does not show});} Monitoring of physical Buttons @overridepublic boolean onKeyDown (int keycode, keyevent event) {switch (keycode) {case Keyevent.keycode_menu: Toast.maketext (Mainactivity.this, "menu key is pressed", Toast.length_long). Show (); Break;case KeyEvent.KEYCODE_BACK:finish (); break;} ReturnSuper.onkeydown (KeyCode, event);}} 


"Android" a variety of pop-up boxes with the menu key, the return key monitoring

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.