editText的使用

editText的限定输入

inputType属性和digits属性

https://www.jianshu.com/p/bd4273c12e5b

1,设置editText可编辑和不可编辑状态

不可编辑状态

editText.setClicked(false);

editText.setFocusable(false);

editText.setFocusableInTouchMode(false);

可编辑状态

editText.setClicked(true);

扫描二维码关注公众号,回复: 4092937 查看本文章

editText.setFocusableInTouchMode(true);

editText.setFocusable(true);

editText.requestFocus();

2更改下划线样式

https://segmentfault.com/a/1190000009507919

方法1,设置一个主题风格并引用

<style name="myEditText" parent="Theme.AppCompat.Light">
//默认的颜色
        <item name="colorControlNormal">@color/color_white</item>
//激活(获取焦点)的颜色
        <item name="colorControlActivated">@color/color_orange</item>
    </style>
android:theme="@style/myEditText"

方法2,更改默认主题的颜色设置,这个是全局变量,意思是更改后,整个应用的对应颜色都会变化

更改自己的theme设置中colorAccent属性即可

3设置密码显示和可见

https://blog.csdn.net/tianhe718/article/details/53099322

方法1

evPasswordValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText ev=(EditText) v;
                if (hasFocus) {
                    ev.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
                } else {
                    ev.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD);
                }
            }
        });

注意,设置密码不可见,需要同时设置InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD才有效

方法2

evPasswordValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText ev=(EditText) v;
                if (hasFocus) {
                    ev.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    ev.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });

4 监听文字变化

(1)监听输入完成后,点击确认

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Log.i(TAG, "onEditorAction: v="+v);
                Log.i(TAG, "onEditorAction: actionId="+actionId);
                Log.i(TAG, "onEditorAction: event="+event);
                return true;
            }
        });

控制台显示

查看

TextView.OnEditorActionListener()

源码参数说明

        
         * Called when an action is being performed.
            当一个动作发生时调用
         *
         * @param v The view that was clicked.    
            (点击的控件)

         * @param actionId Identifier of the action.  This will be either the
         * identifier you supplied, or {@link EditorInfo#IME_NULL
         * EditorInfo.IME_NULL} if being called due to the enter key
         * being pressed.
             (动作标识符)
            可以自定义或者如果由于输入键被按下而被调用,返回默认的通用未指定(EditorInfo.IME_NULL)

         * @param event If triggered by an enter key, this is the event;
         * otherwise, this is null.
            (触发事件的动作ID)
            如果触发的是enter键,event就是输入事件,否则就是null
         * @return Return true if you have consumed the action, else false.
            如果处理该event就返回true,否则就返回false

(2)动态监听text变化

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)                     
            {
                Log.i(TAG, "beforeTextChanged: s="+s);
                Log.i(TAG, "beforeTextChanged: start="+start);
                Log.i(TAG, "beforeTextChanged: count="+count);
                Log.i(TAG, "beforeTextChanged: after="+after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i(TAG, "onTextChanged: s="+s);
                Log.i(TAG, "onTextChanged: start="+start);
                Log.i(TAG, "onTextChanged: before="+before);
                Log.i(TAG, "onTextChanged: count="+count);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i(TAG, "afterTextChanged: s="+s);
            }
        });

控制台输出,输入ww

按照顺序执行3个函数

beforeTextChanged(),
onTextChanged()
afterTextChanged()

可以在

afterTextChanged()

中获取实时editText中的文本内容

String text=s.toString();

猜你喜欢

转载自blog.csdn.net/RungBy/article/details/80846879