【问题标题】:Spell Check in EditText AlertDialog not workingEditText AlertDialog 中的拼写检查不起作用
【发布时间】:2018-04-12 12:16:56
【问题描述】:

我已经为此苦苦思索了几个晚上。我似乎无法让 EditText(设置为 AlertDialog 的视图)在输入文本时自动进行拼写检查,即使我知道拼写检查已启用,因为它在其他地方工作:

错误显示拼写检查不起作用

我看过几篇开发人员希望禁用拼写检查的帖子,但没有任何帖子无法解决。这是因为我正在使用 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>

到警报对话框:

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 吗?任何帮助将不胜感激。

【问题讨论】:

  • 感谢@Navneet 编辑图片。我自己距离能够做到这一点还有2分。 :)

标签: java android android-edittext spell-checking


【解决方案1】:

想出了一个解决办法。由于某种原因,当直接将 EditText 插入构建器,然后为 inputType 应用 textAutoCorrect 标志时,拼写检查将不起作用。

image showing autocorrect working

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

我希望它会自动更正手动放置在其(以前的值)中的内容,但这对我来说已经足够好了。将我的解决方案留给后代。

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2012-02-29
    • 2013-11-04
    • 2018-04-14
    相关资源
    最近更新 更多