Displaying soft keyboard whenever AlertDialog.Builder object is opened

后端 未结 14 1773
半阙折子戏
半阙折子戏 2020-12-08 19:11

My code for opening an input dialog reads as follows:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);  
alert.setTitle(\"Dialog Title\");  
         


        
14条回答
  •  既然无缘
    2020-12-08 20:06

    If you want to pop up dialog box along with soft key pad, so that user could free from tap on edit text inside dialog to show keypad, for example if you are going to take some value from dialog box, then use this simple code, it will solve your problem.

            public void onClick(final View v) 
            {   
                 AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());                 
                  alert.setIcon(R.drawable.smsicon);
                  alert.setTitle(" Secrete Code");  
                  alert.setMessage("Enter a Key for secrete text !");
                  final EditText shft_val = new EditText(v.getContext()); 
                  shft_val.setInputType(InputType.TYPE_CLASS_NUMBER);//changing the keyBoard to No only,restrict the string etc
                  alert.setView(shft_val);
    
         //pOp Up the key pad on Edit Text  focus event
    
                 shft_val.setOnFocusChangeListener(new OnFocusChangeListener()
                 {
                    public void onFocusChange(View arg0, boolean arg1)
                    {  InputMethodManager inputMgr = (InputMethodManager)v.getContext().
                                        getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
                            }
                        });
    
                     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
                     {  
                     public void onClick(DialogInterface dialog, int whichButton) 
                     {
                        //Your specific code... 
                     }
                     });
                     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    
                         public void onClick(DialogInterface dialog, int which) {                       
                             dialog.dismiss();
                             return;   
                         }
                     });
                           alert.show();
                        }
    

提交回复
热议问题