android 中使用EditText的场景还是比较丰富的,无论是是直接使用android提供的原生控件,还是通过自己重新自定义的EditText,因此想记录一下我在真实项目中用过的它的一些使用技巧,后面会持续更新!(PS:有部分代码来自参考文章)
1、让软键盘出现搜索按钮
- 核心代码块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;
}
}
}
2、通过监听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();
}
}
});
}
3、通过属性android:inputType来限制EditText输入的类型
android:inputType="number"
4、通过属性android:digits来规定只能输入的值
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
5、通过属性android:ellipsize来对文本内容的呈现做说明
android:ellipsize="end"
6、规定只能输入中文
/**
* 通过使用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;
}
etDrugName.setFilters(new InputFilter[]{filter});
//如果想要再实现输入字符数量的限制,可以这么写,如果限制字符数为6,就在LengthFilter中传入参数6
// etDrugName.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(6)});
7、只能输入英文字母并且EditText在显示的时候会自动转化为大写
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
//首先是小写转大写的方法
public class UpperCaseTransform extends ReplacementTransformationMethod {
@Override
protected char[] getOriginal() {
char[] aa = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
return aa;
}
@Override
protected char[] getReplacement() {
char[] cc = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
return cc;
}
}
//然后是设置
editText.setTransformationMethod(new UpperCaseTransform());
8、软键盘和自定义键盘(弹框)互斥出现
/**
* 隐藏软件盘
*/
private void hideSoftInputMethod(){
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()){
LogUtil.d(TAG,"软键盘打开了,准备关闭=================");
inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
参考文章
https://blog.csdn.net/lx_asymmetric/article/details/53838165
https://blog.csdn.net/myth13141314/article/details/78124068
https://blog.csdn.net/h7870181/article/details/8332991
https://blog.csdn.net/jyz_2015/article/details/51543318
https://blog.csdn.net/afei__/article/details/51589302