禁用EditText的输入法,但保持光标闪烁 [英] Disable input method of EditText but keep cursor blinking

查看:293
本文介绍了禁用EditText的输入法,但保持光标闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,我想要带android:editable="false"EditText,但是光标闪烁.将可编辑"设置为false后,光标闪烁似乎不起作用.

In my Android application, I want an EditText with android:editable="false" but the cursor blinking. The cursor blinking seems doesn't work after "editable" is set to false.

我只想使用自己的键盘小部件(而不是系统的软键盘),并保持光标闪烁.

I just want to use my own Keyboard widget(not the system's soft keyboard), and keep the cursor blinking.

有什么想法可以实现这一目标吗?

Is there any idea to make that possible?

推荐答案

也许尝试完全忽略xml属性android:editable,然后结合以下内容尝试使用

Maybe try leaving out the xml attribute android:editable entirely and then try the following in combination to

保持光标闪烁,并且防止触摸事件弹出本机IME(键盘).

keep the cursor blinking and prevent touch events from popping up a native IME(keyboard)..

/*customized edittext class 
* for being typed in by private-to-your-app custom keyboard.
* borrowed from poster at http://stackoverflow.com/questions/4131448/android-how-to-turn-off-ime-for-an-edittext
*/
public class EditTextEx extends EditText {
    public EditTextEx(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override 
    public boolean onCheckIsTextEditor() {

        return false; //for some reason False leads to cursor never blinking or being visible even if setCursorVisible(true) was called in code.
    }       
}

第2步 将上述方法更改为return true;

Step 2 change the above method to say return true;

第3步 在上面的类中添加另一个方法.

Step 3 Add another method to above class.

@Override
public boolean isTextSelectable(){
    return true;
}

第4步 在已实例化此类实例并称为viewB的其他位置,我添加了一个新的触摸事件处理程序

Step 4 In the other location where the instance of this class has been instantiated and called viewB I added a new touch event handler

    viewB.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            viewB.setCursorVisible(true);
            return false;
        }
    });

步骤5检查并确保XML和或EditText实例化代码将IME/键盘类型声明为"none".我没有确认相关性,但是我也使用下面的可聚焦属性.

Step 5 Check to make sure XML and or EditText instantiation code declares IME/keyboard type to be 'none'. I didnt confirm relevance, but Im also using the focusable attributes below.

<questionably.maybe.too.longofa.packagename.EditTextEx
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:inputType="none">

很抱歉有这么多xml属性.我的代码全部使用了它们,并在4.2.1中进行了测试,并产生了结果.

Sorry for so many xml attributes. My code uses them all, testing in 4.2.1, and has results.

希望这会有所帮助.

这篇关于禁用EditText的输入法,但保持光标闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆