Android:焦点在EditText上时自动显示软键盘

我正在使用AlertDialog显示一个输入框。 当我调用AlertDialog.show() ,对话框内部的EditText会自动聚焦,但软键盘不会自动显示。 android

如何在显示对话框时自动显示软键盘? (而且没有物理/硬件键盘)。 与按下“搜索”按钮调用全局搜索的方式相似,将自动显示软键盘。 ide


#1楼

若是有人获得: ui

没法从类型Activity对静态方法getSystemService(String)进行静态引用 spa

尝试向getSystemService调用添加上下文code

因此 对象

InputMethodManager imm = 
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

#2楼

是的,你能够用setOnFocusChangeListener来帮助你。 get

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);
        }
    }
});

#3楼

用于显示键盘用途: input

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

隐藏键盘使用: it

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

#4楼

让我指出一些额外的信息到yuku的解决方案,由于我发现很难让这个工做! 如何从AlertDialog.Builder获取AlertDialog对象? 好吧,这是个人alert.show()执行的结果: List

final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
final EditText input = new EditText(getActivity());
alert.setView(input);

// do what you need, like setting positive and negative buttons...

final AlertDialog dialog = alert.show();

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

#5楼

<activity
    ...
    android:windowSoftInputMode="stateVisible" >
</activity>

要么

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
相关文章
相关标签/搜索