Android EditText 点击显示光标不弹出软键盘

今天接到一个功能,需要对金额修改的时候,不调用软键盘,用页面上的数字及删除自己处理,UI如下:

 首先处理上面的EditText,点击的时候不能弹软键盘,但是又要显示光标,代码如下:

        if (android.os.Build.VERSION.SDK_INT > 10) {//4.0以上
            try {
                Class cls = EditText.class;
                Method setShowSoftInputOnFocus;
                setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(et, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    InputMethodManager imm = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
                    }
                }
            }
        });

同时要在EditText得父容器添加:

android:focusable="true"

android:focusableInTouchMode="true"

处理完软键盘和光标之后,还要t获取EditTex光标位置并且插入字符或者删除字符

在光标前添加数据的代码如下:

                int index = et.getSelectionStart();
                Editable editable = et.getText();
                editable.insert(index, "1");

删除光标钱的数据的代码如下:

                int index = et.getSelectionStart();
                Editable editable = et.getText();
                //判断是否还有至少一个字符数据
                if(index > 0){
                    editable.delete(index-1, index);
                }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值