禁用用户输入的自动完成功能(在Java Android应用程序内部)

huangapple go评论82阅读模式
英文:

Disable Autocomplete for User Input (within a Java Android App)

问题

我想在我正在开发的一个应用程序中禁用自动更正和自动完成(来自键盘的单词建议)功能,仅限于某个特定应用。这是一个词汇训练应用,所以我希望用户在输入时不使用任何工具。是否有办法用Java实现这一点?以下是一些相关的代码片段:

用户交互部分:

public void onClick(View view) {
    TextView textView = (TextView) getView().findViewById(R.id.textview);
    final String answer = textView.getText().toString();
    passData(answer);
}

XML部分:

<EditText
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@id/button_first"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="#CCCCCC"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

附注:
我是应用开发的新手,所以您可能会在上面的代码中看到一些问题 - 如果是这样,请告诉我!

英文:

I want to disable auto-correction and auto-complete (word suggestions from the keyboard) on my cellphone
within an app I'm working on.
It's a vocabulary trainer, so I want the User to type the input without using any tools.
Is there a way to do this with Java? Some relevant code snippets:

The user-interaction:

    public void onClick(View view) {
        TextView textView = (TextView) getView().findViewById(R.id.textview);
        final String answer = textView.getText().toString();
        passData(answer);
    }

And the xml:

   &lt;EditText
        android:id=&quot;@+id/textview&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        app:layout_constraintBottom_toTopOf=&quot;@id/button_first&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        android:background=&quot;#CCCCCC&quot;
        android:ems=&quot;10&quot;
        android:inputType=&quot;textPersonName&quot; &gt;

        &lt;requestFocus /&gt;
    &lt;/EditText&gt;

P.S.
I'm new to App Development, so you might see some flaws in my code above - in that case, please tell me!

答案1

得分: 1

以下是已翻译的内容:

在您的 EditText 中添加这段代码,看看是否起作用:

    android:inputType="text|textNoSuggestions"

而不是:

    android:inputType="textPersonName"

这样它会变成这样:

    <EditText
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/button_first"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="#CCCCCC"
        android:ems="10"
        android:inputType="text|textNoSuggestions" >

        <requestFocus />
    </EditText>
英文:

Does it work if you add this to your EditText:

android:inputType=&quot;text|textNoSuggestions&quot;

Instead of:

android:inputType=&quot;textPersonName&quot;

So it becomes this:

&lt;EditText
    android:id=&quot;@+id/textview&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    app:layout_constraintBottom_toTopOf=&quot;@id/button_first&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    android:background=&quot;#CCCCCC&quot;
    android:ems=&quot;10&quot;
    android:inputType=&quot;text|textNoSuggestions&quot; &gt;

    &lt;requestFocus /&gt;
&lt;/EditText&gt;

答案2

得分: 1

通过编程

sampleEdt.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS

文档

> 可与文本及其变体组合使用,指示输入法不应显示任何基于词典的单词建议。对应于InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS。

英文:

Programatically

sampleEdt.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS

Documentation

> Can be combined with text and its variations to indicate that the IME should not show any dictionary-based word suggestions. Corresponds to InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS.

huangapple
  • 本文由 发表于 2020年4月5日 01:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61031742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定