Android-EditText

常用属性

  1. android:hint: 编辑框为空时提示
  2. android:textColorHint: 提示文字的颜色
  3. android:inputType: 输入类型
  4. android:drawableXxxx: 在输入框的指定方位添加图片
  5. android:drawablePadding: 设置图片与输入内容的间距
  6. android:paddingXxxx: 设置内容与边框的间距
  7. android:background: 背景色

取消自动聚焦: 在根布局设置一下两个属性

1
2
3
4
//  可以获得焦点
android:focusable="true"
// 通过触摸获得焦点
android:focusableInTouchMode="true"

inputType的常用类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//文本类型,多为大写、小写和数字符号。
android:inputType="none"
android:inputType="text"
android:inputType="textCapCharacters" 字母大写
android:inputType="textCapWords" 首字母大写
android:inputType="textCapSentences" 仅第一个字母大写
android:inputType="textAutoCorrect" 自动完成
android:inputType="textAutoComplete" 自动完成
android:inputType="textMultiLine" 多行输入
android:inputType="textImeMultiLine" 输入法多行(如果支持)
android:inputType="textNoSuggestions" 不提示
android:inputType="textUri" 网址
android:inputType="textEmailAddress" 电子邮件地址
android:inputType="textEmailSubject" 邮件主题
android:inputType="textShortMessage" 短讯
android:inputType="textLongMessage" 长信息
android:inputType="textPersonName" 人名
android:inputType="textPostalAddress" 地址
android:inputType="textPassword" 密码
android:inputType="textVisiblePassword" 可见密码
android:inputType="textWebEditText" 作为网页表单的文本
android:inputType="textFilter" 文本筛选过滤
android:inputType="textPhonetic" 拼音输入
//数值类型
android:inputType="number" 数字
android:inputType="numberSigned" 带符号数字格式
android:inputType="numberDecimal" 带小数点的浮点格式
android:inputType="phone" 拨号键盘
android:inputType="datetime" 时间日期
android:inputType="date" 日期键盘
android:inputType="time" 时间键盘

常用监听方法

编辑框内容改变监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  search_edit.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) {
// 内容改变后
}
});

软键盘按键监听

1
2
3
4
5
6
7
8
9
10
11
  search_edit.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
// KeyEvent.ACTION_DOWN; 按下按钮时
// KeyEvent.ACTION_UP; 松开按钮时
if (keyEvent.getAction() == KeyEvent.ACTION_UP){
Toast.makeText(getContext(),"松开按钮",Toast.LENGTH_SHORT).show();
}
return false;
}
});