Android使用EditText小技巧汇总
1、隐藏android中EditText自带的的下划线
android:background="@null"
或android:background="@/drawable/bg_edittext_norma.xml"
bg_edittext_norma.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--商品描述的可编辑框-->
<solid android:color="#FFFFFF" />
<corners android:radius="10dip"/>
<stroke
android:width="1dip"
android:color="#BDC7D8" />
</shape>
<EditText
style="?android:attr/textViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:hint="输入用户名"
android:paddingBottom="5dip"
android:paddingTop="5dip" />
2、让软键盘出现搜索按钮
核心代码块1:
这俩个一定要设置,要不然软键盘不会出现搜索
android:imeOptions="actionSearch"
android:singleLine="true"
核心代码块2:
Activity或者Fragment 要实现TextView.OnEditorActionListener接口
public class DrugCatalogueInquiryFragment extends GeneralSocialFragment implements TextView.OnEditorActionListener {
private ClearEditText etDrugName;
etDrugName = xFindViewById(R.id.et_drug_name);
etDrugName.setOnEditorActionListener(this);
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
doWhichOperation(actionId);
return true;
}
private void doWhichOperation(int actionId) {
switch (actionId) {
case EditorInfo.IME_ACTION_SEARCH:
//隐藏项目中弹框
hideSoftInputMethod();
//项目中个性化操作
getEditTextValue();
pageno = 1;
getMedicineListInfoForApp(name,firstWord,type,level,pageno);
break;
default:
break;
}
}
}
android:gravity="left"
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="5"
android:background="#ffffff"
android:paddingLeft="5dp"
android:gravity="left" />
4、修改EditText的光标颜色
android:textCursorDrawable="@null"
//或者
android:textCursorDrawable = "#fff000"
5、通过监听OnFocusChangeListener判断EditText的焦点与否
private void initListener(){
etDrugName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b){
TypeUtils.getInstance( getActivity() ).hideKeyboardView();
}
}
});
etDrugNameOfInitial.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b){
TypeUtils.getInstance( getActivity() ).hideKeyboardView();
}
}
});
}
6、通过属性android:ellipsize来对文本内容的呈现做说明
android:ellipsize="end"
7、通过属性android:digits来规定只能输入的值
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
8、规定只能输入中文
/**
* 通过使用Android源码中的InputFilter接口
*/
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!isChinese(source.charAt(i))) {
return "";
}
}
return null;
}
};
/**
* 判定输入汉字
*
* @param c
* @return
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
评论