开发日记(5) 我们如何让EditText的光标消失呢?
2015-07-29 14:09
501 查看
很多日子没有分享文章,赶项目呢,3人5项目,好烦啊!
正题,这是分享的,原文章:http://www.cnblogs.com/yejiurui/archive/2013/01/02/2841945.html
在我们的应用中,有时候一进入一个页面, EditText默认就会自动获取焦点。弹出输入法框,用户体验很不好,
那么如何取消这个默认行为呢?
ps:这篇文字是一年前写的,现在有网友再问这个问题,我进行重新编辑--2014.05.07,目前有更好的办法,第一种方法局限性很强,大家可以使用第二种方法
第一种方法:.在网上找了好久,有点监听软键盘事件的方法,有调用 clearFouse()方法,但是测试了都不行!在对应的 xml中也找不到相应的属性可以关闭这个默认行为。
后来研究了一下,在其父控件下,添加如下的属性,就可以完美解决:
android:focusable="true"
android:focusableInTouchMode="true"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable
="true" android:focusableInTouchMode="true"
>
<EditText
android:id="@+id/et_enter_msg_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/sent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
/>
</LinearLayout>
第二种方法:直接关闭输入法
调用这个方法体就行了,具体if语句里面的几个参数,我就借用一个网友的日志来写把(在此感谢)
希望有帮助!谢谢观看!
正题,这是分享的,原文章:http://www.cnblogs.com/yejiurui/archive/2013/01/02/2841945.html
在我们的应用中,有时候一进入一个页面, EditText默认就会自动获取焦点。弹出输入法框,用户体验很不好,
那么如何取消这个默认行为呢?
ps:这篇文字是一年前写的,现在有网友再问这个问题,我进行重新编辑--2014.05.07,目前有更好的办法,第一种方法局限性很强,大家可以使用第二种方法
第一种方法:.在网上找了好久,有点监听软键盘事件的方法,有调用 clearFouse()方法,但是测试了都不行!在对应的 xml中也找不到相应的属性可以关闭这个默认行为。
后来研究了一下,在其父控件下,添加如下的属性,就可以完美解决:
android:focusable="true"
android:focusableInTouchMode="true"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable
="true" android:focusableInTouchMode="true"
>
<EditText
android:id="@+id/et_enter_msg_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/sent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
/>
</LinearLayout>
第二种方法:直接关闭输入法
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语句里面的几个参数,我就借用一个网友的日志来写把(在此感谢)
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,则表示输入法打开
希望有帮助!谢谢观看!
相关文章推荐
- MySQL 数据库对服务器端光标的限制
- jQuery往textarea中光标所在位置插入文本的方法
- JS控制一个DIV层在指定时间内消失的方法
- javascript获得光标所在的文本框(text/textarea)中的位置
- 使用focus方法让光标默认停留在INPUT框
- .NET中实现彩色光标、动画光标及自定义光标的方法
- Javascript实现获取及设置光标位置的方法
- 解决EditText、ListView以及GridView同时使用,输入法自动跳出来的方法
- android同时控制EditText输入字符个数和禁止特殊字符输入的方法
- Android中EditText实现不可编辑解决办法
- Android文本输入框(EditText)输入密码时显示与隐藏
- android基础教程之android的listview与edittext冲突解决方法
- Android中EditText和AutoCompleteTextView设置文字选中颜色方法
- Android中实现EditText圆角的方法
- 【MaterialEditText】 Material Design 的 EditText 3ff8
- Linux平台下第三方软件中使用光标键乱码的问题处理
- android之各种布局控件和对话框
- Android开发EditText属性
- 获得输入框光标位置
- Listview 中加上checbox 焦点问题