Android---禁止弹出键盘输入案例

展示:
点击Editext不会弹出键盘,点击ADD_1光标在的Editext 填充1。点击Back则撤回一个单位
在这里插入图片描述

自定义View代码:

public class InputTest extends LinearLayout {

    private static final String TAG = "InputTest:";
    private EditText edt_one,edt_two;

    private Button btn_one,btn_two;

    public InputTest(Context context) {
        this(context,null);
    }

    public InputTest(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public InputTest(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View inflate = LayoutInflater.from(context).inflate(R.layout.input_test, this, true);

        init();
        setDisKeyborad();
        setOnclick();
    }

    private void init() {
        edt_one=findViewById(R.id.edt_one);
        edt_two=findViewById(R.id.edt_two);
        btn_one=findViewById(R.id.btn_one);
        btn_two=findViewById(R.id.btn_two);
    }

    private void setDisKeyborad() {
        edt_one.setShowSoftInputOnFocus(false);
        edt_two.setShowSoftInputOnFocus(false);
    }

    private void setOnclick() {
        //点击增加
        btn_one.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText focusEdt=getFocusEdit();
                if(focusEdt!=null){
                    //获得当前Edittext,这里没有toString(),因为toString后不是同一个对象。Editable操作的是同一个对象
                    Editable text = focusEdt.getText();
                    //获得光标后的位置
                    int selectionEnd = focusEdt.getSelectionEnd();
                    Log.e(TAG,""+selectionEnd);
                    text.insert(selectionEnd,String.valueOf(1));
                }
            }
        });
        //点击删除
        btn_two.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText focusEdt=getFocusEdit();
                if(focusEdt!=null){
                    int selectionEnd = focusEdt.getSelectionEnd();
                    Editable text = focusEdt.getText();
                    if(selectionEnd>0){
                        text.delete(selectionEnd-1,selectionEnd);
                    }
                }
            }
        });
    }
    //获取当前有焦点的输入框(使用要注意判空)
    private EditText getFocusEdit(){
        View focus = findFocus();
        if(focus instanceof EditText){
            return (EditText)focus;
        }
        return null;
    }

}
重点代码:

获取当前有焦点的输入框(使用要注意判空)。findFocus()这个如果没有弹出来要注意当前类是不是在View类里面。

    //获取当前有焦点的输入框(使用要注意判空)
    private EditText getFocusEdit(){
        View focus = findFocus();
        if(focus instanceof EditText){
            return (EditText)focus;
        }
        return null;
    }

获得当前Edittext,这里没有toString(),因为toString后不是同一个对象。Editable操作的是同一个对象

//获得当前Edittext,这里没有toString(),因为toString后不是同一个对象。Editable操作的是同一个对象
Editable text = focusEdt.getText();

获得光标后的位置(改为Start为获得光标前的位置)

//获得光标后的位置
int selectionEnd = focusEdt.getSelectionEnd();

自定义View布局文件:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edt_one"
        android:hint="one"/>
    <EditText
        android:layout_width="match_parent"
        android:hint="two"
        android:id="@+id/edt_two"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_one"
        android:text="add_1"/>
    <Button
        android:id="@+id/btn_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="back"/>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值