And*_*rew 131 android virtual-keyboard
我有一个Activity
带EditText
,一个按钮和一个ListView
.目的是在中键入搜索屏幕EditText
,按下按钮并使搜索结果填充此列表.
这一切都很完美,但虚拟键盘表现得很奇怪.
如果我点击EditText
,我会得到虚拟键盘.如果我单击虚拟键盘上的"完成"按钮,它就会消失.但是,如果在单击虚拟键盘上的"完成"之前单击我的搜索按钮,则虚拟键盘会停留,我无法摆脱它.单击"完成"按钮不会关闭键盘.它将"完成"按钮从"完成"更改为箭头并保持可见.
谢谢你的帮助
Pau*_*rat 300
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
Run Code Online (Sandbox Code Playgroud)
事后我把它放好了onClick(View v)
.
你需要导入android.view.inputmethod.InputMethodManager
;
单击按钮时键盘会隐藏.
And*_*rew 59
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
return true;
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
Pra*_*dro 28
使用以下代码
your_button_id.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
}
});
Run Code Online (Sandbox Code Playgroud)
pix*_*xel 13
您应该OnEditorActionListener
为EditView 实现
public void performClickOnDone(EditView editView, final View button){
textView.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
hideKeyboard();
button.requestFocus();
button.performClick();
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
你隐藏键盘:
public void hideKeybord(View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
Run Code Online (Sandbox Code Playgroud)
您还应该使用按钮来触发键盘隐藏 onClickListener
现在单击虚拟键盘上的"完成"按钮将执行相同操作 - 隐藏键盘并执行单击操作.
Ash*_*kol 11
在按钮单击事件中添加以下代码:
InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Run Code Online (Sandbox Code Playgroud)
由于您只有一个编辑文本,因此只需在按钮单击内调用该编辑文本的操作,其余部分由系统处理.如果您有多个edittext,那么这将不会那么高效,因为您必须首先获得有针对性的edittext.但在你的情况下,它将完美地工作
myedittext.onEditorAction(EditorInfo.IME_ACTION_DONE)
Run Code Online (Sandbox Code Playgroud)
对于活动,
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
对于Fragments,使用getActivity()
.getActivity()getSystemService();
.getActivity()getCurrentFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
这个解决方案对我来说很完美:
private void showKeyboard(EditText editText) {
editText.requestFocus();
editText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
editText.setSelection(editText.getText().length());
}
private void closeKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
128405 次 |
最近记录: |