EditText隐藏系统软键盘以及常用属性

首先我的需求是进入页面有个EditText,点击要弹出自定义的视图不能弹出系统的软键盘。于是我想到了搜索前任造好的轮子原则开始上网搜索。发现关于EditText隐藏软键盘的方法网上有很多了,但是我试了几种以后发现没有任何效果(难道是我打开的方式不对 )最后还是找到了方法来解决。好了总结一些常用的方法以及最后采用的方法。

第一种方法:在XML文件下添加:
android:focusable=“true”
android:focusableInTouchMode=“true”
实验效果直接无效
第二种方法:直接关闭输入法
在onCreate中加上:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
第一次进去的时候仍然弹出了系统的软键盘
第三种方法:在Edittext中设置
EditText对象.setInputType(InputType.TYPE_NULL);
软键盘终于不弹出来了,但是光标不见了。。。我去,这是逗我么。
最后找到的方法

 //隐藏系统自带的软键盘
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            mPetBirth.setInputType(InputType.TYPE_NULL);
        } else {
            getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setSoftInputShownOnFocus;
                setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                setSoftInputShownOnFocus.setAccessible(true);
                setSoftInputShownOnFocus.invoke(mPetBirth, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

这样可以隐藏软键盘,光标也存在,但是当你输入文字之后发现光标一直在第一位。。。这个可以再加一个监听文本变化的内容监听器

  // TODO Auto-generated method stub
        mPetBirth.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
    
               mPetBirth.setSelection(mPetBirth.length());
            }
        });

好了,到这一步就完美解决了我的问题了。
接下来总结一些常用的EditText属性

 <EditText
        android:id="@+id/pet_born"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/px30dp"
        android:background="@null"
        android:hint="  Please enter your pet's birthday"
        android:textColor="@color/color_2e2e2e"
        android:textColorHint="@color/color_9e9e9e"
        android:textCursorDrawable="@drawable/edit_text_cursor"
        android:maxLines="1"
        android:textSize="@dimen/px30sp" />

android:textColorHint="@color/color_9e9e9e"提示文本的颜色
android:textCursorDrawable="@drawable/edit_text_cursor"自定义的光标,你也可以换成你自己想要的图形
android:background="@null"这一句很重要,这是去掉默认的EditText自带的下划线样式。
好了其余的属性也不需要我解释了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值