Android Edittext详解

1

android:password="true"老版本时,我们常用此来实现输入是密码。后来此方法过期了。Android推出了新的方法实现。

android:inputType="textPassword"现在google推荐使用这个方法了。
##inputType属性释义##
android:inputType=”none”
android:inputType=”text”文本
android:inputType=”textCapCharacters” 字母大写
android:inputType=”textCapWords” 首字母大写
android:inputType=”textCapSentences” 仅第一个字母大写
android:inputType=”textAutoCorrect” 自动完成
android:inputType=”textAutoComplete” 自动完成
android:inputType=”textMultiLine” 多行输入
android:inputType=”textImeMultiLine” 输入法多行(如果支持)
android:inputType=”textNoSuggestions” 不提示
android:inputType=”textUri” 网址
android:inputType=”textEmailAddress” 电子邮件地址
android:inputType=”textEmailSubject” 邮件主题
android:inputType=”textShortMessage” 短讯
android:inputType=”textLongMessage” 长信息
android:inputType=”textPersonName” 人名
android:inputType=”textPostalAddress” 地址
android:inputType=”textPassword” 密码
android:inputType=”textVisiblePassword” 可见密码
android:inputType=”textWebEditText” 作为网页表单的文本
android:inputType=”textFilter” 文本筛选过滤
android:inputType=”textPhonetic” 拼音输入
//数值类型
android:inputType=”number” 数字
android:inputType=”numberSigned” 带符号数字格式
android:inputType=”numberDecimal” 带小数点的浮点格式
android:inputType=”phone” 拨号键盘
android:inputType=”datetime” 时间日期
android:inputType=”date” 日期键盘
android:inputType=”time” 时间键盘

重点内容

android:textColorHint="" //设置提示hint信息的颜色。
android:digits="1234567890." //可以指出要支持的字符。比如要限制只能输入数字

设置EditText不可输入

android:focusable="false"
//Java中1.设置为不可编辑
editText.setFocusable(false);
editText.setFocusableInTouchMode(false);
//Java中1.设置为可编辑
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
editText.requestFocus();

一键删除内容
A:自定义

B:设置drawableRight,然后获取EditText监听,在右侧区域时响应事件

editText.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) {
                //有文字就显示一键删除。
                Drawable drawableLeft = getResources().getDrawable(R.drawable.plg_user);
                drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
                if (s.length() > 1) {
                    Drawable drawable = getResources().getDrawable(R.drawable.clear_pressed);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    editUsername.setCompoundDrawables(drawableLeft, null, drawable, null);
                }else {
                    editUsername.setCompoundDrawables(drawableLeft, null, null, null);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        
        editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Drawable drawable = editUsername.getCompoundDrawables()[2];
                if (drawable == null) {
                    return false;
                }
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (event.getX() > editUsername.getWidth() - editUsername.getPaddingRight() - drawable.getIntrinsicWidth()) {
                    editUsername.setText("");
                }
                return false;
            }
        });

设置Edittext获取焦点并弹出软键盘

public static void showSoftInputFromWindow(Activity activity, EditText editText) {
    editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
    editText.requestFocus();
 activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
  }

评论时@人

Android 如何优雅地实现@人功能?——仿微博、仿QQ、仿微信、零入侵、高扩展性 鸿神强推,@方案

大小写自动转换

/*要将输入的小写字母自动转化为大写字母并显示在EditText上,比较简便的方法是设置EditText的setTransformationMethod*/
    private fun initEtNum() {
        etCarNum.transformationMethod = object : ReplacementTransformationMethod() {
            override fun getOriginal(): CharArray {
                return charArrayOf('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')
            }

            override fun getReplacement(): CharArray {
                return charArrayOf('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')
            }
        }
    }

推荐文章

http://www.tuicool.com/articles/bQriiu

http://blog.sina.com.cn/s/blog_509927e501018bry.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值