您的位置:首页 > 移动开发 > Android开发

Android Edittext 开发常见问题总结(焦点、输入、键盘弹出等)

2015-08-18 13:21 826 查看
  Edittext在android的开发中经常需要使用,以下列举其开发中遇到的一些常见情况:

  

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);


其他问题,待补充。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: