【发布时间】:2014-04-03 07:29:10
【问题描述】:
我有一个选项卡视图,每个选项卡有一个 Activity,当我从第一个选项卡(仅显示带有搜索(编辑文本)的列表)切换到具有 TextView 的第二个选项卡时,软键盘仍然那里。我想让它消失。有关如何解决此问题的任何建议?
【问题讨论】:
标签: android textview android-softkeyboard android-keypad
我有一个选项卡视图,每个选项卡有一个 Activity,当我从第一个选项卡(仅显示带有搜索(编辑文本)的列表)切换到具有 TextView 的第二个选项卡时,软键盘仍然那里。我想让它消失。有关如何解决此问题的任何建议?
【问题讨论】:
标签: android textview android-softkeyboard android-keypad
试试这个,android:configChanges="orientation|keyboardHidden"
<activity
android:name="YourActivityname"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="@style/Theme.MyAwesomeTheme" >
</activity>
另一种方法是强制它们隐藏,如下所示。
将此代码放在您的 utils 类中以保持代码井井有条。
public static void hideKeyboard(Activity activity) {
try {
InputMethodManager inputManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Exception e) {
// Ignore exceptions if any
Log.e("KeyBoardUtil", e.toString(), e);
}
}
您可以将此方法称为Utils.hideKeyboard(your activity.this);
【讨论】:
试试这个
public static void hideSoftInput(View _v,Context _c){
if(_v.getWindowToken() != null){
InputMethodManager inputManager = (InputMethodManager) _c.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(_v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
【讨论】:
jusr 将此方法粘贴到您的班级中,这将在触摸方法和软键盘隐藏上进行
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}
隐藏起来
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
【讨论】:
只要你触摸你定义的字段以外的地方,键盘就会消失。
//to hide keyboard
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]);
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}
【讨论】:
给你一个答案,它会起作用的
yourtextview.setRawInputType(Configuration.KEYBOARDHIDDEN_YES);
【讨论】: