Android使用EditText Android使用EditText小技巧汇总

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android使用EditText Android使用EditText小技巧汇总

双木L   2021-05-06 我要评论
想了解Android使用EditText小技巧汇总的相关内容吗,双木L在本文为您仔细讲解Android使用EditText的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,EditText,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;
        }
    }

}

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

到这里就结束啦。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们