在咱们的应用中,有时候一进入一个页面, EditText默认就会自动获取焦点。弹出输入法框,用户体验很很差,java
那么如何取消这个默认行为呢?android
ps:这篇文字是一年前写的,如今有网友再问这个问题,我进行从新编辑--2014.05.07,目前有更好的办法,第一种方法局限性很强,你们能够使用第二种方法ide
第一种方法:.在网上找了很久,有点监听软键盘事件的方法,有调用 clearFouse()方法,可是测试了都不行!在对应的 xml中也找不到相应的属性能够关闭这个默认行为。测试
后来研究了一下,在其父控件下,添加以下的属性,就能够完美解决:this
android:focusable="true"
android:focusableInTouchMode="true"spa
举例以下:日志
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:focusable
="true" android:focusableInTouchMode="true"code
> <="@+id/et_enter_msg_content"="wrap_content"="wrap_content"="1" /> <="@+id/sent"="wrap_content"="wrap_content"="@string/send" /> </LinearLayout>
第二种方法:直接关闭输入法orm
1
2
3
4
5
6
7
8
|
private
void
closeInputMethod() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
boolean
isOpen = imm.isActive();
if
(isOpen) {
// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示
imm.hideSoftInputFromWindow(mobile_topup_num.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
|
调用这个方法体就好了,具体if语句里面的几个参数,我就借用一个网友的日志来写把(在此感谢)xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
、方法一(若是输入法在窗口上已经显示,则隐藏,反之则显示)
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);
[java] view plaincopy
imm.hideSoftInputFromWindow(view.getWindowToken(),
0
);
//强制隐藏键盘
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,则表示输入法打开
|
好了,祝你们玩的开心