androidのEditTex详细使用
1. EditText多行输入显示
EditText 输入默认是一行显示,当一行输入超过了屏幕宽度时,自动增加显示高度。效果android2.3 看的明显,android4.0不容易看出。如果想要多行显示。
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3" >
</EditText>
这里android:minLines="3",即为文本框显示为3行,显示效果如下图:
2.使光标输入从顶部开始,可以添加:android:gravity="Top"
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:gravity="top" >
</EditText>
3.EditText输入字符长度限制
EditText可以在XML和代码中设置字数限制,假如输入不超过50字符。第一种:在XML中EdidText组件可以添加属性: android:maxLength =“50”
第二种:在java代码中:
EditText editText=(EditText)findViewById(.....);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50)});
第三种:
- EditText editText = (EditText) findViewById(R.id.entry);
- editText.addTextChangedListener(new MaxLengthWatcher(50, editText));
/*
* 监听输入内容是否超出最大长度,并设置光标位置
* */
public class MaxLengthWatcher implements TextWatcher {
private int maxLen = 0;
private EditText editText = null;
public MaxLengthWatcher(int maxLen, EditText editText) {
this.maxLen = maxLen;
this.editText = editText;
}
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
Editable editable = editText.getText();
int len = editable.length();
if(len > maxLen){
int selEndIndex = Selection.getSelectionEnd(editable);
String str = editable.toString();
//截取新字符串
String newStr = str.substring(0,maxLen);
editText.setText(newStr);
editable = editText.getText();
//新字符串的长度
int newLen = editable.length();
//旧光标位置超过字符串长度
if(selEndIndex > newLen){
selEndIndex = editable.length();
}
//设置新光标所在的位置
Selection.setSelection(editable, selEndIndex);
}
}
}
4. EditText文本输入类型属性介绍
android:inputType= 指定该文本框输入类型, 对应java代码中:使用 setRawInputType(int) 设置
属性值有:date -->日期输入 number-->数值 numberPassword-->数字密码 phone-->电话号码
5. 让EditText 失去焦点,点击时键盘不会弹出
在其添加如下的属性android:focusable="false"
editText.setFocusableInTouchMode(false);
6. EditText得到和失去焦点时,自定义处理内容
当android的Edittext得到/失去焦点时,需要自定义一些处理内容时,需要对EditText对象的Focus进行监听处理。在Activity中,做以下处理:
1.取得EditText对象。
2.为取得的对象添加setOnFocusChangeListener(),重写里面的方法。
EditText loginPwd = (EditText) findViewById(R.id.editText);
editText.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
// 此处为得到焦点时的处理内容
} else {
// 此处为失去焦点时的处理内容
}
}
});
7. EditText 添加背景图片实现,如图
分析实现,分别使用两个EditText。使用layout_weight 属性进行分割显示,
<LinearLayout
android:layout_width="230dip"
android:layout_height="140dip"
android:background="@drawable/login_box"
android:orientation="vertical">
<com.example.administrator.myfirstdemo.ClearEditText
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawableLeft="@drawable/mail"
android:drawablePadding="5dp"
android:ems="10"
android:hint="用户名"
android:imeOptions="actionDone"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:singleLine="true"
android:inputType="text" >
<requestFocus />
</com.example.administrator.myfirstdemo.ClearEditText>
<com.example.administrator.myfirstdemo.ClearEditText
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawableLeft="@drawable/password"
android:drawablePadding="5dp"
android:hint="密码"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:password="true"
android:singleLine="true" />
</LinearLayout>
8. EditText 输入框左边添加图片
使用 android:drawableLeft="@drawable/password" 即可
调整 该图片与收入光标的距离时候,,加入 android:drawablePadding="5dp" 即可
调整 该图片跟输入光标距离边框距离,,加入 android:paddingLeft="10dip" android:paddingRight="10dip" 适当调整
9. EditText 添加图片背景后,去除原生输入框线框背景
使用android:background="@null" 即可
10. EditText 添加背景图片效果,如图
光标以及字体距离边框距离: android:padding="10dip" 背景图片: android:background="@drawable/button_click"
11. EditText 弹出键盘后,使其不遮挡输入框
需要在androidmanifesh.xml中添加 android:windowSoftInputMode="adjustPan" 自适应布局。与第5冲突解决。android:windowSoftInputMode="adjustPan" android:configChanges="orientation|keyboardHidden" 即可
12.启动activity,EditText获得焦点但不弹出键盘
在androidmanifesh.xml中添加:android:windowSoftInputMode="adjustUnspecified|stateHidden" android:configChanges="orientation|keyboardHidden"
或者 添加属性:android:focusable="true" android:focusableInTouchMode="true"
13. 默认EditText初始化自带字符串,让光标显示字符串最后
edittext.setText(str);后面加上 edittext.setSelection(str.length());
14.EditText光标颜色更改
android:textCursorDrawable,这个属性是用来控制光标颜色的android:textCursorDrawable="@null","@null"作用是让光标颜色和text color一样