拼写检查EditText AlertDialog不起作用

时间:2018-04-11 20:29:30

标签: java android android-edittext spell-checking

我已经在这几个晚上摸不着头脑了。我似乎无法将EditText(设置为AlertDialog的视图)设置为在输入文本时自动拼写检查,即使我知道拼写检查已启用,因为它在其他地方工作:

错误显示拼写检查无法正常工作

enter image description here

我已经看过几个帖子,其中开发人员想要禁用拼写检查,但没有任何一个它没有工作的任何解决方案。这是禁用的,因为我正在使用AlertDialog(其他地方的EditTexts正在运行),如果是这样,除了我可以尝试的AlertDialog之外,是否有任何解决方法或任何其他解决方案?

这是我的代码:

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final EditText input = new EditText(getActivity());
input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES|InputType.TYPE_TEXT_FLAG_AUTO_CORRECT|InputType.TYPE_TEXT_FLAG_MULTI_LINE|InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE); //I have tried all combinations of InputType options, including no options.
input.setSingleLine(false);
input.setMaxLines(6);
input.setText("Some Default Text");
input.setSelectAllOnFocus(true);
input.requestFocus();
builder.setView(input);
builder.show();

更新

我刚试过膨胀以下布局文件:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtJournalSummaryLabel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Please summarize your experience studying:"
        android:textAlignment="center"
        android:textSize="12sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/txtJournalNewSummary"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textCapSentences|textMultiLine"
        android:maxLines="6"
        android:minLines="3"
        android:singleLine="false"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtJournalSummaryLabel" />
</android.support.constraint.ConstraintLayout>

到AlertDialog:

View view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_journal_summary_prompt, null);
final EditText txtInput = view.findViewById("Some Default Text");
txtInput.setText(this.journalTitles.get(0));
builder.setView(view);

但是AlertDialog仍然没有显示拼写错误。这是因为视图没有附加到父Activity / Fragment?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

找出解决方案。出于某种原因,当直接将EditText插入构建器,然后对inputType应用textAutoCorrect标志时,拼写检查将不起作用。

image showing autocorrect working

解决方案是使用EditText创建一个布局文件(请参阅上面的修改过的问题),但确保在xml资源文件中启用textAutoCorrect标志,将膨胀的视图应用于AlertDialog构建器,然后看它工作。

我希望它会自动自动更正手动放置在它们(之前的值)中的内容,但这对我来说效果很好。离开我的解决方案为后代。