首先我的需求是进入页面有个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自带的下划线样式。
好了其余的属性也不需要我解释了。