【问题标题】:How to make cursor visible with inputype set to null?如何在 inputype 设置为 null 的情况下使光标可见?
【发布时间】:2018-01-05 09:54:09
【问题描述】:

我正在创建一个简单的计算器应用程序,它有一个 Edittext 和一些按钮来接受用户的输入。

<EditText
    android:id="@+id/editText"
    style="@style/editTextStyle"
    android:text="0"
    android:inputType="none"
    android:textIsSelectable="true" />

下面是java 代码。

editText = (EditText) findViewById(R.id.editText);
editText.setInputType(InputType.TYPE_NULL);

我将输入类型设置为 null,以便当用户点击 edittext 时,默认系统键盘保持隐藏状态。但这也使光标不可见。当我在Edittext 中插入一些文本并点击它时,光标位置会发生变化,但光标不可见。

有没有办法让光标在输入类型设置为 null 时可见??

更新:

我尝试使用以下代码。

editText = (EditText) findViewById(R.id.editText);
editText.setInputType(InputType.TYPE_NULL);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);

此解决方案在 Nougat 之前适用于 android 版本。当我点击 Edittext 时,系统键盘保持隐藏状态,光标可见。但在奥利奥,这个解决方案不起作用。当我点击 Editext 时,系统键盘会弹出。

有没有办法在光标可见时隐藏系统键盘??

【问题讨论】:

  • 不需要设置null,只要编辑字段有焦点就会显示光标。
  • 改变这个editText.setInputType(InputType.TYPE_NULL);

标签: android android-edittext android-keypad


【解决方案1】:

你应该使用:

editText.setTextIsSelectable(true);

【讨论】:

    【解决方案2】:

    在您的Activity 中使用它:
    editText.setFocusable(true);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT‌​_INPUT_STATE_ALWAYS_HIDDEN);

    【讨论】:

      【解决方案3】:

      你不需要 InputType.TYPE_NULL 在您拥有编辑文本框的活动的 onCreate() 中:

          protected void onCreate(Bundle savedInstanceState) {
      
      mAmountText = (EditText) findViewById(R.id.amount);
      
      // This will disable the Soft Keyboard from appearing by default
      this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
      
      // Provide focus to the Amount box (since it is the top edittext box inside RelativeLayout)
      mAmountText.requestFocus();
      
      }
      

      在您的布局文件中(称为 note_edit.xml):

      <?xml version=”1.0″ encoding=”utf-8″?>
      
      <RelativeLayout
      android:id=”@+id/main_layout”
      android:layout_width=”fill_parent”
      android:layout_height=”fill_parent”
      android:background=”#ffffffff”
      android:descendantFocusability=”beforeDescendants”
      android:focusableInTouchMode=”true”
      >
      
      <EditText
      android:id=”@+id/amount”
      android:textColor=”#ff0066ff”
      android:numeric=”decimal”
      android:gravity=”left”
      android:hint=”$0.00″
      android:textSize=”48sp”
      android:layout_height=”72dp”
      android:layout_width=”fill_parent”
      android:windowSoftInputMode=”stateHidden”
      >
      </EditText>
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-31
        • 2017-01-11
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        • 2012-05-04
        相关资源
        最近更新 更多