焦点改变时如何隐藏虚拟键盘
在开发过程中碰到了一个需求:在点击搜索框后,再点击别的地方,让虚拟键盘隐藏
在焦点改变时,隐藏虚拟键盘。在 listView 中添加焦点监听。其中 mEditText 为所输入的文本。
import android.view.MotionEvent;
private void initListView() {
mAdapter = WaterMarkLocationAdapter.getInstance(this);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView adapter,
View view, int position, long id) {
String text = (String)mAdapter.getItem(position);
if (text == null) return;
//mEditText.setText(text);
save2pref(text);
WaterMarkLocationActivity.this.finish();
}
});
mListView.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
hideInputSoft();
}
}
});
mListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.requestFocus();
}
return false;
}
});
}
public void hideInputSoft() {
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null)
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
那么显示输入法虚拟键盘也是一样:
InputMethodManager imm = (InputMethodManager)(getSystemService(Context.INPUT_METHOD_SERVICE));
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>