【问题标题】:Why doesn't the blinking cursor appear when my edittext is enabled?为什么启用我的编辑文本时不出现闪烁的光标?
【发布时间】:2020-11-04 11:57:57
【问题描述】:

我有一个按钮和一个 EditText,当我按下按钮时,光标应该出现在 EditText 中并且应该显示软键盘。我已经尝试了 SO 的一堆建议,但没有任何效果。到目前为止,这是我的逻辑:

<EditText
    android:id="@+id/typed_word"
    android:maxLength="10"
    android:enabled="false"
    android:inputType="text"
    android:layout_below="@+id/word_list"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    android:textStyle="bold"
    android:textSize="25sp"
    android:textColor="@color/colorPrimaryDark"
    android:layout_margin="10dp"
    android:hint="@string/type_hint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

当按下按钮时,EditText 中会发生这种情况:

    start_timer_button.setOnClickListener(new OnClickListener(){
        public void onClick(View v)
        {
            InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            assert inputMgr != null;
            inputMgr.showSoftInput(typed_word, InputMethodManager.SHOW_IMPLICIT);

            reset_button.setEnabled(true);
            typed_word.requestFocus();
            word_list.setText("");
            typed_word.setText("");
            typed_word.setEnabled(true);
            text_str = "";
            start_timer_button.setEnabled(false);
            enableSettings = false;
        }
    });

谁能看到我做错了什么?这两天我一直在纠结这个问题。

【问题讨论】:

    标签: android android-edittext android-cursor


    【解决方案1】:

    事实上,我能够解决这个问题的唯一方法是创建一个自定义光标:

    custom_cursor.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
      <size android:width="3dp" />
      <solid android:color="@color/colorPrimaryDark"  />
    </shape>
    

    <EditText
        android:id="@+id/typed_word"
        android:maxLength="10"
        android:textCursorDrawable="@drawable/custom_cursor"
        android:enabled="false"
        android:inputType="text"
        android:layout_below="@+id/word_list"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        android:textSize="25sp"
        android:textColor="@color/colorPrimaryDark"
        android:layout_margin="10dp"
        android:hint="@string/type_hint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 2011-04-09
      相关资源
      最近更新 更多