Android development in progress-Use Dialog prompt to obtain input information

Source: Internet
Author: User

 

Prompt dialog refers to a pop-up form that provides an input box for users to input data. The program needs to read the data and then perform subsequent processing. In JavaScript, window. prompt will pop up a dialog box for the user to enter information, and then the program will be paused in the input box. After the user input is complete and click OK, the program will be executed, so the prompt dialog is in the synchronous form. But in Android, dialog is asynchronous, so how can we achieve a similar synchronization effect?

This requires callback. (Ajax is also asynchronous, so callback is also required. The callback helps us solve the asynchronous operation but achieves the synchronization effect .) In Android, we also use alertdialog to create a promptdialog. Let's take the following steps:

  1. Create a view object with the control and layout provided by the view (we usually convert the XML layout file to the view object through layoutinflater)
  2. Create a builder object
  3. Set the view object created in step 1 to builder.
  4. Create button and set callback function
  5. Generate a dialog object based on the previously configured builder.
  6. Call dialog. Show () to display the generated dialog.

Therefore, we first create an XML layout file, which will be placed in the dialog. Prompt_view.xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <textview Android: Id = "@ + ID/promptmessage" <br/> Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" <br/> Android: layout_marginleft = "20dip" Android: layout_marginright = "20dip" <br/> Android: text = "Enter the following information here:" Android: gravity = "Left" <br/> Android: textappearance = "? Android: ATTR/textappearancemedium "/> <br/> <edittext Android: Id =" @ + ID/edittext_prompt "<br/> Android: layout_height =" wrap_content "Android: layout_width = "fill_parent" <br/> Android: layout_marginleft = "20dip" Android: layout_marginright = "20dip" <br/> Android: scrollhorizontally = "true" Android: autoText = "false" <br/> Android: Capitalize = "NONE" Android: gravity = "fill_horizontal" <br/> Android: textappearance = "? Android: ATTR/textappearancemedium "/> </P> <p> </linearlayout> 

Then convert the XML layout file to view:

Layoutinflater li = layoutinflater. From (this); <br/> View view = Li. Inflate (R. layout. prompt_view, null ); 

Place the view in builder, set the button and callback function, call the create method of builder to create a dialog object, and display the dialog object.

Public void onshowpromptdialog (view v) {<br/> // view object to be displayed in dialog <br/> layoutinflater li = layoutinflater. from (this); <br/> View view = Li. inflate (R. layout. prompt_view, null); </P> <p> alertdialog. builder = new alertdialog. builder (this); <br/> builder. settitle ("prompt test"); <br/> builder. seticon (R. drawable. icon); <br/> // put the inflate view in the dialog <br/> builder. setview (View); <br/> builder. setpositivebutton ("OK", this); <br/> builder. setnegativebutton ("cancel", this); <br/> builder. create (). show (); </P> <p>} 

Here we set the callback method to be processed by the activity agent, so the activity needs implements dialoginterface. onclicklistener:

Public class demodialog extends activity implements dialoginterface. onclicklistener {</P> <p>} 

Then add the unimplemented onclick method for the activity:

@ Override <br/> Public void onclick (dialoginterface diich, int which) {</P> <p> If (which = dialog. button_positive) {</P> <p> alertdialog ad = (alertdialog) dialog; <br/> edittext t = (edittext) AD. findviewbyid (R. id. edittext_prompt); <br/> toast. maketext (this, T. gettext (). tostring (), toast. length_long) <br/>. show (); <br/>}</P> <p>} 

In this case, we will determine whether the selected dialog button is a confirmation button. If yes, we will find the input View Control (edittext) in dialog ), then, obtain the content entered by the user and display it with toast. The onclick method regards the currently clicked dialog as the first parameter, and then the clicked button ID as the second parameter to call the callback function, because the basic class interface dialoginterface of all dialog s of the type of the first parameter needs to be forcibly converted to the alertdialog object before the view can be searched out. The second parameter button ID has three values, alertdialog can have a maximum of three buttons. The dialog class provides static variable values for the three buttons, therefore, you can compare the button ID with the three static variable values to obtain which button the user clicks.

In Android, when using promptdialog, note that dialog in Android is in asynchronous mode. Therefore, you cannot find the input box in dialog immediately after displaying the dialog, then obtain the input value. Because the user sees that the input box is not filled with the value, the user must obtain the input value and perform subsequent processing in the callback method.

 

Related Article

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.