[Android Basics] control response method of the custom setView method of AlertDialog, alertdialogsetview

Source: Internet
Author: User

[Android Basics] control response method of the custom setView method of AlertDialog, alertdialogsetview

When Android uses AlertDialog to bring up a dialog box, if you need to customize the dialog box interface, you need to use setView to set the interface. At this time, there are usually controls on the interface that need to respond or record results, you need to find the control. The following is an example of finding and responding to a control in the Custom dialog box:

LayoutInflater layoutInflater = LayoutInflater. from (mContext); View newPlanDialog = layoutInflater. inflate (R. layout. new_plan_dialog, (ViewGroup) findViewById (R. id. linearLayout_newplan); AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this ). setTitle ("Add new Plan "). setIcon (R. drawable. ic_launcher ). setView (newPlanDialog); builder. setPositiveButton ("OK", new android. content. dialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub // Toast. makeText (getApplicationContext (), start. toString () + ":" + end. toString (), 2000 ). show (); String content = text. getText (). toString (); Log. d ("Content", content); Integer. toString (start. getCurrentHour () + ":" + Integer. toString (start. getCurrentMinute (); Log. d ("EndTime", Integer. toString (end. getCurrentHour () + ":" + Integer. toString (end. getCurrentMinute (); dialog. dismiss () ;}}); builder. setNegativeButton ("cancel", new android. content. dialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub dialog. dismiss () ;}}); AlertDialog ad = builder. create (); // create dialog box ad. show (); // you need to display it first before you can find the control start = (TimePicker) ad. findViewById (R. id. timepicker_start); end = (TimePicker) ad. findViewById (R. id. timepicker_end); text = (EditText) ad. findViewById (R. id. editText_content );

The xml file on the Interface contains two time controls and an editing box.
Note that the show dialog box is required before you can find the control.

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.