Displaying soft keyboard whenever AlertDialog.Builder object is opened

后端 未结 14 1765
半阙折子戏
半阙折子戏 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 19:52

    I create an AlertDialog and use a custom view which contains an EditText. And I want the soft keyboard to be shown when the dialog is shown and to be hidden whether the user clicks OK button or somewhere outside the dialog.

    This piece of code is from androidx.perference.PreferenceDialogFragmentCompat and I clean up a little.

    final AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setTitle(mDialogTitle)
            .setPositiveButton(mPositiveButtonText, null)
            .setNegativeButton(mNegativeButtonText, null);
    
    View contentView = LayoutInflater.from(context).inflate(resId, null);
    
    mEditText = contentView.findViewById(android.R.id.edit);
    
    /**
     * From PreferenceDialogFragmentCompat.needInputMethod
     * <p>Note: If your application targets P or above, ensure your subclass manually requests
     * focus (ideally in {@link #onBindDialogView(View)}) for the input field in order to
     * correctly attach the input method to the field.
     */
    mEditText.requestFocus();
    
    builder.setView(contentView);
    
    final Dialog dialog = builder.create();
    dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
    // This is not from PreferenceDialogFragmentCompat and I add it.
    // It seems the soft keyboard won't get dismissed on some old devices without this line.
    dialog.setOnDismissListener {
        dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
    
    dialog.show();
    

    You don't need to modify Manifest. It automatically focuses to the EditText and will be dismissed whether you click dialog action buttons or somewhere outside the dialog.

    0 讨论(0)
  • 2020-12-08 19:53

    try using inputBox

    inputBox.getWindow().setSoftInputMode( 
                   WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
    0 讨论(0)
  • 2020-12-08 19:54

    try using view

    v.getWindow().setSoftInputMode( 
               WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
    0 讨论(0)
  • 2020-12-08 19:57

    This is in response to miannelle.

    The following method is called when a menu option is selected:

    private void addNote() {
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.textentryalertdialog);
        dialog.setTitle("Add note");
        TextView msgText = (TextView) dialog.findViewById(R.id.messagetext);
        msgText.setText("Whatever prompt you want");
        final EditText inputLine = (EditText) dialog.findViewById(R.id.my_edittext);
        Button okButton = (Button) dialog.findViewById(R.id.OKButton);
        okButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                dialog.dismiss();
                // app specific code
            }           
        });
        Button cancelButton = (Button) dialog.findViewById(R.id.CancelButton);
        cancelButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                dialog.dismiss();
            }           
        });
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        dialog.show();
    }
    

    The textentryalertdialog.xml file defines a linear layout containing

    TextView android:id="@+id/messagetext" ...

    EditText android:id="@+id/my_edittext" ...

    Button android:id="@+id/OKButton" ...

    Button android:id="@+id/CancelButton" ...

    I hope this helps.

    0 讨论(0)
  • 2020-12-08 19:58

    I know, this Question is really old, but since I've tried about 20 different so called 'solutions', I will post, what actually only worked for me finally.

    This answer is based on the answer of Pir Fahim Shah, who pointed me in the right direction (Thanks):

    Make sure, you put this in the onCreate of your activity, so that forced keyboards are being hidden, when dialog is closed:

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    

    then create a dialog like this:

        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("title");
        final EditText input = new EditText(this);
        input.setText("text");
        input.setSelection(input.getText().length()); // set cursor to end
        builder.setView(input);
        input.setOnFocusChangeListener(new OnFocusChangeListener()  {
           public void onFocusChange(View v, boolean hasFocus) { 
               if(hasFocus) {
                   InputMethodManager inputMgr = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                   inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
               }
           }
        });
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // do something here
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("Cancel", null);
        builder.show();
    
    0 讨论(0)
  • 2020-12-08 20:02

    .setView() will automatically bring up the keyboard in a dialog. Note the "final" on the EditText itself. IMPORTANT!

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Random Title...");
    alert.setMessage("Type a name");
    
    final EditText input = new EditText(this);
    
    //Set input focus (which opens the android  soft keyboard)
    alert.setView(input);   
    input.setHint("My Name is...");
    //Set keyboard type
    input.setInputType(InputType.TYPE_CLASS_TEXT); 
    
    //add button onclick handlers...
    
    alert.show();
    

    I think a lot of people are really overdoing it with all the functions... This is fairly universal and work great. And it won't bloat your code.

    0 讨论(0)
提交回复
热议问题