Android 手动显示和隐藏软键盘 点击空白处隐藏键盘
2017-03-21 16:25
609 查看
1、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示)
2、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)
3、调用隐藏系统默认的输入法
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
WidgetSearchActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
(WidgetSearchActivity是当前的Activity)
4、获取输入法打开的状态
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开
二、======================================================================
点击空白处隐藏键盘
1.实现方法一:
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
eg:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout style="@style/rcs_actionbar" >
2.实现方法二:
通过给当前界面布局文件的父layout设置点击事件(相当于给整个Activity设置点击事件),
在事件里进行键盘隐藏
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/traceroute_rootview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:clickable="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
java代码:
3.实现方法三:
通过dispatchTouchEvent每次ACTION_DOWN事件中动态判断非EditText本身区域的点击事件,然后在事件中进行屏蔽。
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否则所有的组件都不会有TouchEvent了
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
isShoudHideInput(View v,MotionEvent e)方法:
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = { 0, 0 };
//获取输入框当前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {
// 点击的是输入框区域,保留点击EditText的事件
return false;
} else {
return true;
}
}
return false;
}
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
2、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view,InputMethodManager.SHOW_FORCED); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
3、调用隐藏系统默认的输入法
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
WidgetSearchActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
(WidgetSearchActivity是当前的Activity)
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是当前的Activity)
4、获取输入法打开的状态
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开
二、======================================================================
点击空白处隐藏键盘
1.实现方法一:
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
eg:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout style="@style/rcs_actionbar" >
2.实现方法二:
通过给当前界面布局文件的父layout设置点击事件(相当于给整个Activity设置点击事件),
在事件里进行键盘隐藏
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/traceroute_rootview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:clickable="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
java代码:
@Override public void onClick(View v) { switch (v.getId()) { case R.id.traceroute_rootview: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); break; } }
3.实现方法三:
通过dispatchTouchEvent每次ACTION_DOWN事件中动态判断非EditText本身区域的点击事件,然后在事件中进行屏蔽。
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return super.dispatchTouchEvent(ev);
}
// 必不可少,否则所有的组件都不会有TouchEvent了
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
isShoudHideInput(View v,MotionEvent e)方法:
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = { 0, 0 };
//获取输入框当前的location位置
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {
// 点击的是输入框区域,保留点击EditText的事件
return false;
} else {
return true;
}
}
return false;
}
相关文章推荐
- Android 手动显示和隐藏软键盘 android 隐藏显示输入法键盘
- Android点击空白处隐藏键盘
- android点击空白处隐藏键盘(亲测)
- android中点击任意空白处隐藏键盘的方法
- Android.EditText点击时,隐藏系统弹出的键盘,显示出光标
- 【Android】 点击空白处隐藏(收起)键盘
- Android 点击空白处隐藏键盘
- Android.edittext点击时,隐藏系统弹出的键盘,显示出光标
- Android.EditText点击时,隐藏系统弹出的键盘,显示出光标
- android 键盘显示与隐藏
- android系统键盘隐藏与显示问题
- Android 键盘的显示与隐藏
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
- android 手机不需要phonegap 对键盘的隐藏与显示
- android隐藏以及显示软键盘以及不自动弹出键盘的方法
- android显示和隐藏键盘
- android中隐藏与显示软键盘以及不自动弹出键盘的方法
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
- android中ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法
- Android控件 动态初始化 动态加载 EditText显示光标 隐藏键盘