Android EditText控件使用

本文详细探讨了Android中EditText的使用,包括隐藏默认样式、防止首次启动时自动弹出输入法、触发编辑框显示、控制编辑框是否可编辑、设置输入过滤规则以及监听输入完成事件等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0 本文主要涉及

Android EditText控件的一些实际使用中的需求实现。

1 隐藏默认样式

去除框样式 设置属性 android:background="@null"
去除光标 设置属性 android:textCursorDrawable="@null"

2 首次进入页面不需要弹出输入法

给父view设置 android:focusableInTouchMode="true"

3 触发弹出编辑框

theEditText.requestFocus();
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(theEditText, 0);

4 是否可以编辑控制

a 设置默认不可编辑
设置属性 android:focusable="false" //还可以设置并触发点击事件
b 代码动态设置
theEditText.setEnabled(false);//同时不可点击
theEditText.setEnabled(true);//恢复

5设置输入过滤

a设置inputType 
属性设置 android:inputType="number"
代码设置 theEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
b属性设置 android:digits="1234567890"
c输入过滤(source是当前输入的字符串不包括已经输入的)

theEditText.setFilters(new InputFilter[]{new InputFilter()
        {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
            {
                return source.toString().replaceAll("\n|\r|\\s*", "");
            }
        }});

d 输入监听(注意设置条件不然会死循环)

theEditText.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)
            {
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                String str = s.toString();
                String str1 = str.replaceAll("\n|\r|^\\s*|\\s*$", "");
                if (!str.equals(str1))
                {
                    addressEditText.setText(str1);
                    addressEditText.setSelection(str1.length());
                }
            }
        });

6 输入完成监听

theEditText.setOnEditorActionListener(new TextView.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                //do something...
                return false;
            }
        });

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值