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

Android EditText常见方法总结

2016-01-21 00:00 489 查看

1、设置焦点监听事件

et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
//有焦点
}else {
//无焦点
}
}
});


2、设置键盘监听事件

et.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keycode, KeyEvent keyEvent) {
if (keycode == KeyEvent.KEYCODE_ENTER) {
//回车事件
}
return false; //返回false会继续执行回车换行,返回true不会执行回车换行
}
});

et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {

// 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 效果:
return false;
}
});


3、监听键盘响应事件

et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// 输入内容之前
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// 输入内容的时候
}

@Override
public void afterTextChanged(Editable editable) {
// 输入内容之后
}
});


4、限制输入的类型

*在布局文件中设置

<EditText
android:id="@+id/variableValue"
......
android:inputType="number" />

*在代码中设置

et.setInputType(inputType);

相关的参数:

相关参数

5、默认不要焦点

android:focusable="true"
android:focusableInTouchMode="true"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  EditText