edittext 软键盘上方_Android:焦点在EditText上时自动显示软键盘

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

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

#1楼

如果有人得到:

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

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

所以

InputMethodManager imm =

(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

#2楼

是的,你可以用setOnFocusChangeListener来帮助你。

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楼

用于显示键盘用途:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

隐藏键盘使用:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(view.getWindowToken(),0);

#4楼

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

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楼

...

android:windowSoftInputMode="stateVisible" >

要么

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值