[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.