Displaying soft keyboard whenever AlertDialog.Builder object is opened

后端 未结 14 1767
半阙折子戏
半阙折子戏 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:13

    Hi Prepbgg there is actually an alternative way to what you've done. I actually had to use mine within my ArrayAdapter. What i did was pass the context of the activity into the arrayadapter then call it to access getWindow() something like this:

    NoteArrayAdapter(Activity _activity, int _layout, ArrayList<Note> _notes, Context _context) {
      callingNoteListObj = (NoteList) _context;
    }
    
    // withiin getView
    
    callingNoteListObj.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    
    // within parent activity's onCreate()
    thisObject = this;
    
    //within my parent activity's fillData() (NoteList)
    adapter =  new NoteArrayAdapter(activity, R.layout.channel_note_list_item, noteList, thisObject);
    
    0 讨论(0)
  • 2020-12-08 20:18

    I think you almost had it working in your original question. Try creating a final AlertDialog to call getWindow() on, e.g.

    // Create the dialog used to modify the mailbox alias
    final AlertDialog dialog = alert.create();
    
    inputBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                dialog.getWindow().setSoftInputMode( 
                   WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
    

    I've tested this code and the keyboard now appears automatically on most of my devices, including:

    • Samsung Galaxy Tab OS 2.2
    • Samsung Galaxy S OS 2.1
    • HTC Sensation OS 2.3.4

    Some other comments on this solution:

    1) Check if you've got anything in your XML already to request the focus, as that may stop this code working (according to Ted in the question you link to).

    2) This code doesn't seem to work on my HTC G2 running OS 2.3.4. I guess this is because it has a physical keyboard, and maybe the WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE option doesn't work with it?

    I also tried SOFT_INPUT_STATE_VISIBLE (without the ALWAYS), but that stopped the keyboard appearing automatically.

    3) You may also want to add code so the user can press the Done button on the keyboard to submit the changes, e.g.

    inputAlias.setOnKeyListener(new OnKeyListener()
    {
      @Override
      public boolean onKey(View v, int keyCode, KeyEvent event)
      {
        if (keyCode == KeyEvent.KEYCODE_ENTER &&
            inputAlias.isFocused() &&
            inputAlias.getText().length() != 0)
        {
          // Save the new information here
    
      // Dismiss the dialog
          dialog.dismiss();
          return true;
        }
        return false;
      }
    });
    
    0 讨论(0)
提交回复
热议问题