EditView显示和隐藏软键盘(黑屏问题)

光标的颜色(有些手机是不支持光标颜色的设置的比如:华为手机)

在drawable文件夹下新建login_editview_cursor_color.xml,内容如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/color_fa1155" />
    <size android:width="1dp"/>

</shape>

输入的editView内容:
<EditText
    android:id="@+id/login_verify_mobile"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:layout_gravity="center_vertical"
    android:background="#ffffff"
    android:gravity="center_vertical"
    android:hint="请输入手机号"
    android:inputType="number"
    android:maxLines="1"
    android:textCursorDrawable="@drawable/login_editview_cursor_color"
    android:paddingLeft="15dp"
    android:paddingStart="15dp"
    android:textColor="#222222"
    android:textColorHint="#CCCCCC"
    android:textSize="14sp" />
如果是在包含fragment的Activity中尽量不要使用 android:windowSoftInputMode="adjustResize|stateHidden",隐藏软键盘,容易出现弹出和隐藏的黑屏
现象。可以在代码中这是editView 的焦点为false来隐藏软键盘。
mMobileNumber.setCursorVisible(false);//隐藏光标
mMobileNumber.setFocusable(false);//失去焦点
mMobileNumber.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mMobileNumber.setFocusable(true);
        mMobileNumber.setCursorVisible(true);
        mMobileNumber.setFocusableInTouchMode(true);
        mMobileNumber.requestFocus();
        //打开软键盘
        InputMethodManager inputManager = (InputMethodManager) mMobileNumber.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(mMobileNumber, 0);
    }
});

还有一种在弹软键盘和隐藏软件盘的时候,有fragment的情况,也可以更换theme来进行。参考http://blog.csdn.net/hjj395139289/article/details/53309417
你可以使用一个 ImageView 来控制 EditText 显示隐藏密码。在点击 ImageView 时,切换 EditText 的输入类型即可。 首先,在布局文件中添加一个 ImageView 和一个 EditText,例如: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/password_edittext" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Password" android:inputType="textPassword"/> <ImageView android:id="@+id/show_password_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_show_password"/> </LinearLayout> ``` 然后,在代码中获取 ImageViewEditText,并在 ImageView 的点击事件中切换 EditText 的输入类型: ``` ImageView showPasswordImageView = findViewById(R.id.show_password_imageview); EditText passwordEditText = findViewById(R.id.password_edittext); showPasswordImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int inputType = passwordEditText.getInputType(); if (inputType == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) { passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); showPasswordImageView.setImageResource(R.drawable.ic_show_password); } else { passwordEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); showPasswordImageView.setImageResource(R.drawable.ic_hide_password); } // move cursor to the end of the text passwordEditText.setSelection(passwordEditText.getText().length()); } }); ``` 这里需要注意的是,当切换输入类型时,EditText 中的光标位置会丢失,所以需要在切换后将光标移动到文本末尾。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值