Android Edittext 开发常见问题总结(焦点、输入、键盘弹出等)
2015-08-18 13:21
826 查看
Edittext在android的开发中经常需要使用,以下列举其开发中遇到的一些常见情况:
1、焦点得到/失去事件监听
我们需要在android的Edittext得到/失去焦点时,处理一些事件时,需要对EditText对象的Focus进行监听处理。
2、强制失去焦点
在某些情况下,我们不希望进入界面初始化时Edittext自动获取焦点,有一种简便方法:此时我们只需在xml文件中使其强制失去焦点,具体在其父控件中加上:
android:focusable=”true”
android:focusableInTouchMode=”true”
代码如下:
3、输入特殊格式的字符
密码文本框:密码输入是Android应用常用的功能,通过配置EditText的android:password=”true”就可以实现这一密码输入功能
电话输入框:专门输入电话号码的文本框,通过设置android:phoneNumber=”true”就可以把EditText变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了。
纯数字:EditText为我们提供了android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数)和decimal(浮点数)。
4、Enter键图标
软键盘的Enter键默认显示的是“完成”文本。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:
actionUnspecified :未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED
actionNone:没有动作,对应常量EditorInfo.IME_ACTION_NONE
actionGo:去往,对应常量EditorInfo.IME_ACTION_GO
actionSearch :搜索,对应常量EditorInfo.IME_ACTION_SEARCH
actionSend :发送,对应常量EditorInfo.IME_ACTION_SEND
actionNext: 下一个,对应常量EditorInfo.IME_ACTION_NEXT
actionDone: 完成,对应常量EditorInfo.IME_ACTION_DONE
5、键盘弹出问题
5.1 此处给出Android EditText不弹出输入法焦点问题的总结。
5.2 强制弹出键盘:
其他问题,待补充。
1、焦点得到/失去事件监听
我们需要在android的Edittext得到/失去焦点时,处理一些事件时,需要对EditText对象的Focus进行监听处理。
//定义 Edittext mEdt = (EditText) findViewById(R.id.my_edittext); mEdt .setOnFocusChangeListener(mFocusChangeListener); //监听 private OnFocusChangeListener mFocusChangeListener = new OnFocusChangeListener() { @Override public void onFocusChange(View arg0, boolean isFocus) { // TODO Auto-generated method stub if(isFocus) { // 此处进行得到焦点时的事件处理 } else { // 此处进行失去焦点时的事件处理 } } };
2、强制失去焦点
在某些情况下,我们不希望进入界面初始化时Edittext自动获取焦点,有一种简便方法:此时我们只需在xml文件中使其强制失去焦点,具体在其父控件中加上:
android:focusable=”true”
android:focusableInTouchMode=”true”
代码如下:
<RelativeLayout android:layout_width="fill_parent" android:layout_height="35dp" android:focusable="true" android:focusableInTouchMode="true" android:orientation="horizontal" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText style="@style/EditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/button" android:background="@null" > </EditText> </RelativeLayout>
3、输入特殊格式的字符
密码文本框:密码输入是Android应用常用的功能,通过配置EditText的android:password=”true”就可以实现这一密码输入功能
电话输入框:专门输入电话号码的文本框,通过设置android:phoneNumber=”true”就可以把EditText变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了。
纯数字:EditText为我们提供了android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数)和decimal(浮点数)。
4、Enter键图标
软键盘的Enter键默认显示的是“完成”文本。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:
actionUnspecified :未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED
actionNone:没有动作,对应常量EditorInfo.IME_ACTION_NONE
actionGo:去往,对应常量EditorInfo.IME_ACTION_GO
actionSearch :搜索,对应常量EditorInfo.IME_ACTION_SEARCH
actionSend :发送,对应常量EditorInfo.IME_ACTION_SEND
actionNext: 下一个,对应常量EditorInfo.IME_ACTION_NEXT
actionDone: 完成,对应常量EditorInfo.IME_ACTION_DONE
5、键盘弹出问题
5.1 此处给出Android EditText不弹出输入法焦点问题的总结。
5.2 强制弹出键盘:
mEidText = (EditText) findViewById(R.id.edittext); mEidText.setFocusableInTouchMode(true); mEidText.requestFocus(); Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { InputMethodManager inputManager = (InputMethodManager) mEidText.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(mEidText, 0); } },800);
其他问题,待补充。
相关文章推荐
- 开发Android硬件访问服务
- Android源码分析之Handler机制
- Activity 生命周期
- Android对话框的几种形式
- 关于AndroidStudio导入项目报错的问题
- android Fragment
- Android中fragment切换标题操作
- Android之Handler源码深入分析
- IT蓝豹,android技术特效分享
- Android Studio一些简单设置
- Android自定义View-自动加载更多的ListView
- Android日期选择器
- android手机屏幕横竖屏切换禁止调用生命周期函数方法
- Android Drawable 那些不为人知的高效用法
- Android BitmapShader 实战 实现圆形、圆角图片
- Android ListView的上拉刷新,下拉加载功能(pull - to -refesh)
- Android Xfermode 实战 实现圆形、圆角图片
- Android中加入百度地图SDK
- android开发 缩放到指定比例的尺寸
- android 自定义ViewGroup实现可记载并呈现选择的ListView