Android UI之EditText常用技巧整理

1、简介

本文记录的是Android开发中很常用的EditText 控件的很常用的使用技巧,后续会陆续整理这方面的内容也是更好的熟悉将来的开发。

2、具体案例与效果

2.1  关于光标

2.1.1 对于单个EditText的光标颜色设置:

android:textCursorDrawable="@drawable/shape_cursor_login"

shape_cursor_login.xml 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="@dimen/common_dimension_1"/>
    <solid android:color="@color/orange_add_pond"/>
</shape>

 这样就可以修改单个控件的光标颜色。

 2.1.2 对于一个活动或者整个Application的光标颜色的设置

如下,所示在需要的活动或者全局设备相对应的光标颜色即可。

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/accent</item>
        <!--这里设置的全局的游标颜色 -->
        <item name="android:textCursorDrawable">@drawable/style_cursor_black</item>
        <item name="gallery.arrow_down.icon">@drawable/arrow_down</item>
        <!--标题上拉箭头-->
        <item name="gallery.arrow_up.icon">@drawable/arrow_up</item>
        <!--图片列表勾选样式-->
        <!-- 这里可以定义全局的属性 不过需要在attrs里进行申明-->
    </style>

2.1.3 光标的显示位置

有时候是有默认的字的,那么这个时候就应当设置光标在该段字之后才显示出具体的光标。 

mEdContent.setSelection(mContent.length());

2.2  关于EditText的输入内容监听

 addTextChangedListener( ),设置监听。

        mEtKeyInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mTvWordNum.setText(String.valueOf(AppConst.NUM_KEY_INPUT-mEtKeyInput.getText().length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

2.3 根据焦点改变状态改变而切换控件状态

如下为一个根据焦点状态改变而设置选中线的颜色的这么一个小的功能。代码很easy如下代码块所示。

        mEtOldPwd.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                mVLineOldPwd.setBackgroundColor(UIUtils.getColor(R.color.orange_add_pond));
            }else {
                mVLineOldPwd.setBackgroundColor(UIUtils.getColor(R.color.line));
            }
        });

        mEtPwd.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                mVLinePwd.setBackgroundColor(UIUtils.getColor(R.color.orange_add_pond));
            }else {
                mVLinePwd.setBackgroundColor(UIUtils.getColor(R.color.line));
            }
        });

2.4 设置密码属性时,可见和不可见状态切换

        // 密码可见的状态改变事件
        mIvSeePwd.setOnClickListener(v -> {
            if (mEtPwd.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
                // 由可见变为不可见
                mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mIvSeePwd.setImageResource(R.mipmap.ic_eye_normal);
            } else {
                // 由不可见变为可见
                mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mIvSeePwd.setImageResource(R.mipmap.ic_eye_pressed);
            }
            /* 光标移动到的位置*/
            mEtPwd.setSelection(mEtPwd.getText().toString().trim().length());
        });

猜你喜欢

转载自blog.csdn.net/crazyZhangxl/article/details/81634760
今日推荐