sda*_*bet 75 android android-edittext
在我看来,我有一个搜索EditText,我想以编程方式触发字段上的单击事件的行为,即将焦点放在文本字段上并在必要时显示软键盘(如果没有可用的硬键盘).
我试过了field.requestFocus().该字段实际上获得焦点但不显示软键盘.
我试过了field.performClick().但那只调用该字段的OnClickListener.
任何的想法 ?
pgs*_*rom 143
好先生,试试这个:
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
Run Code Online (Sandbox Code Playgroud)
我不确定,但某些手机(某些旧设备)可能需要这样做:
final InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)
小智 55
这是适合我的代码.
edittext.post(new Runnable() {
public void run() {
edittext.requestFocusFromTouch();
InputMethodManager lManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
lManager.showSoftInput(edittext, 0);
}
});
Run Code Online (Sandbox Code Playgroud)
而已!请享用 ;)
将下面的代码 为我工作,后其他两个答案 ,我没有工作:
@Override
public void onResume() {
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
//passwordInput.requestFocus(); <-- that doesn't work
passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen
}
Run Code Online (Sandbox Code Playgroud)
哪里ShowKeyboard是
private class ShowKeyboard implements Runnable {
@Override
public void run() {
passwordInput.setFocusableInTouchMode(true);
// passwordInput.requestFocusFromTouch();
passwordInput.requestFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
输入成功后,我还要确保隐藏键盘
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getView().getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
从技术上讲,我只是在运行软键盘显示请求之前增加了300 ms的延迟。奇怪吧?也更改requestFocus()为requestFocusFromTouch()。
编辑:不要使用requestFocusFromTouch()它给启动器提供触摸事件。坚持下去requestFocus()。
EDIT2:在对话框(DialogFragment)中,使用以下命令
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)
代替
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79131 次 |
| 最近记录: |