2012-03-21 90 views
7

我在Android中使用AlertDialog.Builder快速提示用户输入文字。该对话框出现并且效果很好,但用户必须点击EditText字段才能加载软键盘。有没有办法打开键盘,并在打开对话框时关注焦点?这里是我的代码:默认焦点和键盘到Android AlertDialog中的EditText

final Map<String,Object> rowData = itemList.get(mPosition); 
        final EditText input = new EditText(searchList.getContext()); 
       input.requestFocus(); 


       input.setSingleLine(); 
       final AlertDialog dialog = new AlertDialog.Builder(searchList.getContext()) 
       .setTitle(StringUtils.getSafeString(rowData.get("label"))) 
       .setView(input) 
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         rowData.put("value", StringUtils.getSafeString(input.getText())); 
         searchList.invalidateViews(); 

        } 
       }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         // Do nothing. 
        } 
       }).create(); 
       dialog.show(); 

回答

12

使用下面的代码中。它为我工作。

editText.setOnFocusChangeListener(new OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      editText.post(new Runnable() { 
       @Override 
       public void run() { 
        InputMethodManager inputMethodManager= (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 
       } 
      }); 
     } 
    }); 
    editText.requestFocus(); 
+0

非常感谢,完美的作品! – 2017-10-30 23:52:22

-2
在你的XML布局

呼叫

<requestFocus/> 

默认的EditText

<EditText 
android:blabla 
.... 
<requestFocus/> 
/> 
+1

的AlertDialog.Builder不使用XML的布局,只需要在在代码中创建一个EditText。我已经尝试过在多个地方调用生成的EditText的请求焦点,而且它看起来并不像我希望的那样工作。这不是这个问题的工作解决方案。 – cain 2012-03-22 12:21:00

0

InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 隐藏键盘使用:

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 012.htmlSoftInputFromWindow(view.getWindowToken(),0);

,或者尝试下面的代码,但你必须在requestFocus()方法或设置为您的EditText

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     } 
    } 
}); 
5

隐藏键盘时,编程设置侧重于一个EditText在Android对话框。

我也有这个问题,这是一个非常简单的修复 - 这是我建议的解决方案。虽然它为我工作在DialogFragments上,但我没有看到为什么它不适用于您的情况。

基本上没有触发软键盘,因为视图是以编程方式创建的。实际的修复程序简单地把该线路onCreateDialog方法:

dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

从Android文档上DialogFragments

如果用户集中在一个EditText,软键盘将 自动出现。为了迫使我们的 编程焦点发生,我们将其称为 getDialog()。getWindow()。setSoftInputMode()。请注意,许多先前在对话框中完成的Window 操作仍然可以在DialogFragment中完成 ,但您必须调用getDialog()。getWindow() 而不是仅调用getWindow()。

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    //setup your dialog builder and inflate the view you want here 
    ... 
    //Make sure your EditText has the focus when the dialog is displayed 
    edit.requestFocus(); 
    //Create the dialog and save to a variable, so we can set the keyboard state 
    Dialog dialog = builder.create(); 
    //now, set to show the keyboard automatically 
    dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
    return dialog; 
} 
+1

要走的路!这应该是被接受的答案。我使用了'getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);'在我的AppCompatDialog子类的构造函数中。 – 2015-12-15 18:45:22

+0

dialog.getWindow()可能返回null – 2017-06-03 01:39:26

0

使用自定义视图如果你需要它往往

public class RequestFocusEditText extends AppCompatEditText { 
    private RequestFocusEditText self; 

    public RequestFocusEditText(final Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.self = this; 

     setOnFocusChangeListener(new OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       post(new Runnable() { 
        @Override 
        public void run() { 
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_IMPLICIT); 
       } 
      }); 
     } 
    }); 
    requestFocus(); 
    } 
}