The EditText in Android custom dialog cannot pop up the keyboard.
Recently, my independently developed project "Medical Doctor meeting" is in beta testing and will soon be deployed in various application markets. It is a pity that the previous projects have not been shelved for some reasons. So recently I have been in a very good mood.
Today, we are working on a new project, an APP designed for lawyers and customers. There is a need for a custom dialog box. This knowledge point is actually very simple, it is the result:
However, after I finished writing it leisurely, I found that the embedded EditText in the Custom dialog box could not get the focus, and the soft keyboard could not pop up. It was depressing. When no EditText exists in the previously developed software, everything would work normally, no hidden pitfalls were found. Is a custom dialog box I wrote earlier:
Here we will solve this problem:
1. layout file of the dialog box:
2. Custom dialog code
// Pop up custom dialog LayoutInflater inflater = LayoutInflater. from (OnlineActivity. this); View view = inflater. inflate (R. layout. hl_law_dialog, null); // Dialog box final dialog Dialog = new Dialog (OnlineActivity. this); dialog. getWindow (). setBackgroundDrawable (new ColorDrawable (0); dialog. requestWindowFeature (Window. FEATURE_NO_TITLE); dialog. show (); // set the screen width to WindowManager windowManager = getWindowManager (); Display display Display = windowManager. getdefadisplay display (); WindowManager. layoutParams lp = dialog. getWindow (). getAttributes (); lp. width = (int) (display. getWidth (); // you can specify the dialog width. getWindow (). setAttributes (lp); dialog. getWindow (). setContentView (view); final EditText et_obj = (EditText) view. findViewById (R. id. et_obj); // The final EditText et_name = (EditText) view. findViewById (R. id. et_name); // name: final EditText et_phone = (EditText) view. findViewById (R. id. et_phone); // call TextView TV _go = (TextView) view. findViewById (R. id. TV _go); // confirm the delegate final ImageView iv_close = (ImageView) view. findViewById (R. id. iv_close); // confirm the delegate
The above is the correct method, so where is my mistake?
My previous statement was:
// Dialog box final dialog Dialog = new AlertDialog. builder (OnlineActivity. this ). create (); dialog. getWindow (). setBackgroundDrawable (new ColorDrawable (0); dialog. requestWindowFeature (Window. FEATURE_NO_TITLE); dialog. show ();
Therefore, you only need to replace AlertDialog with Dialog to solve this problem.
Imagine:
1. What is the effect if this code is not added?
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
The title bar appears ugly.
2. What is the effect if this code is not added?
WindowManager windowManager = getWindowManager (); Display display = windowManager. getdefadisplay display (); WindowManager. layoutParams lp = dialog. getWindow (). getAttributes (); lp. width = (int) (display. getWidth (); // you can specify the dialog width. getWindow (). setAttributes (lp );
The dialog box is so narrow.