【问题标题】:TextEdit with cursor but no keyboard带光标但没有键盘的文本编辑
【发布时间】:2016-04-01 22:56:15
【问题描述】:

我想制作一个TextEdit,它的光标移动和可选择的文本,但没有键盘(如计算器应用程序)。 我尝试了一种解决方案,它在出现后立即隐藏键盘。但这真的很慢,它会在消失之前出现片刻。

有没有更好的解决方案(android 5.0.1 及以上)?

android:windowSoftInputMode="stateHidden"(清单内的活动标签)不起作用

EditText et = (EditText) findViewById(R.id.editText);
et.setInputType(InputType.TYPE_NULL);
et.setCursorVisible(true);
et.setTextIsSelectable(true);

要么。

提前致谢

【问题讨论】:

  • 不想输入数据为什么要光标?
  • @SagarNayak 我想要一些按钮在光标所在的位置插入文本!

标签: android android-layout


【解决方案1】:

尝试在您的活动中添加以下代码。

android:windowSoftInputMode="stateHidden"

喜欢,

<activity android:name=".MainActivity" 
          android:windowSoftInputMode="stateHidden" />

编辑

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/transparent"
    android:focusable="true"
    android:focusableInTouchMode="true">
</FrameLayout>

<EditText
    android:id="@+id/searchAutoCompleteTextView_feed"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:inputType="text" />

使用FrameLayout 请求焦点。这不会将焦点添加到EditText

【讨论】:

  • 这很奇怪,但添加后键盘仍然显示!
  • 对不起这个问题不相关。但我已经编辑了我的清单 xml,并且更改没有出现在 app.xml 中。我该怎么办?
  • 我认为你的回答是正确的。但问题是。我清理/重建/重新运行项目,并且清单不会在发送到我手机的应用程序中更改。
  • 删除您的 genbin 目录并重新构建。
  • @ChintanRathod Bhai group ma add kar ne Gujarati android developer ma or koi admin ne ke
【解决方案2】:

我使用这种方法在我的 Utils 类中隐藏键盘:

public static void hideKeyboard(@NonNull Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    View f = activity.getCurrentFocus();
    if (null != f && null != f.getWindowToken() && EditText.class.isAssignableFrom(f.getClass()))
        imm.hideSoftInputFromWindow(f.getWindowToken(), 0);
    else
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

【讨论】:

  • 应该如何使用?
  • 在您的 utils 类或类似类中声明它并调用方法:Class.hideKeyboard(getActivity()); (在 Fragment 中)或 Class.hideKeyboard(this); (在活动中)
【解决方案3】:

添加

android:windowSoftInputMode="stateAlwaysHidden"

在您的活动清单文件中。

【讨论】:

  • 我记得这个,以前用过。但是现在如果我在我的 Galaxy s6 上编译和运行,即使在我的活动标签中设置了这个属性,键盘也会显示出来。我不明白为什么!
猜你喜欢
  • 1970-01-01
  • 2019-05-19
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 2019-06-08
相关资源
最近更新 更多