Android使用EditText小技巧汇总

龙旋

共 4359字,需浏览 9分钟

 ·

2021-05-01 21:41

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; } }
}


3、多行EditText的时候会出现光标在中间的问题:


关键代码
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" />

像这种。这是什么原因造成的呢?用来EdittText默认是gravity是center.就是从中间对齐。我们把他改成left啊top啊就OK了。

4、修改EditText的光标颜色


在使用EditText的XML 文件中加入一个属性:
android:textCursorDrawable="@null"//或者android:textCursorDrawable = "#fff000"

这个属性是用来控制光标颜色的,"@null" 是作用是让光标颜色和text color一样,当然也可以修改成你自己的颜色。

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; }

到这里就结束啦。
浏览 9
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报