0

我正在尝试在 android studio 中为 editext 实现拼写检查。框架应验证用户输入的最后一个单词。问题是拼写检查框架没有返回建议并且没有显示任何错误。

这是我得到的输出示例:,,(2)

以下代码:

public class MainActivity extends AppCompatActivity implements 
SpellCheckerSession.SpellCheckerSessionListener {

EditText editText;
SpellCheckerSession mScs;
TextView tv1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    editText = (EditText) findViewById(R.id.user_message);
    tv1=findViewById(R.id.textView);
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!(editText.getText().toString().equalsIgnoreCase(""))) {
                String[] text = editText.getText().toString().split(" ");
                if (mScs != null) {
                    mScs.getSuggestions(new TextInfo(text[text.length-1]), 3); //last word
                } else {
                    // Show the message to user
                    Toast.makeText(getApplicationContext(), "Please turn on the spell checker from setting", Toast.LENGTH_LONG).show();
                    ComponentName componentToLaunch = new ComponentName("com.android.settings",
                            "com.android.settings.Settings$SpellCheckersSettingsActivity");
                    Intent intent = new Intent();
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.setComponent(componentToLaunch);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    try {
                        getApplicationContext().startActivity(intent);
                    } catch (ActivityNotFoundException e) {
                        // Error
                    }
                }

            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

}
public void onResume() {
    super.onResume();
    final TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
    mScs = tsm.newSpellCheckerSession(null, null, this, true);
}

public void onPause() {
    super.onPause();
    if (mScs != null) {
        mScs.close();
    }
}

public void onGetSuggestions(final SuggestionsInfo[] arg0) {
    final StringBuilder sb = new StringBuilder();
    Log.d("LENGTH", String.valueOf(arg0.length));

    for (int i = 0; i < arg0.length; ++i) {
        // Returned suggestions are contained in SuggestionsInfo
        final int len = arg0[i].getSuggestionsCount();
        sb.append('\n');

        for (int j = 0; j < len; ++j) {
            sb.append("," + arg0[i].getSuggestionAt(j));
            Log.d("suggestion", arg0[i].getSuggestionAt(j));
        }

        sb.append(" (" + len + ")");
    }
    runOnUiThread(new Runnable() {
        public void run() {
            tv1.append(sb.toString());
        }
    });
}

@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
    // TODO Auto-generated method stub
}

}

提前致谢。

4

1 回答 1

0

您是否在 Mainfest.xml 中实现了它并创建了 spellchecker.xml 来指定语言?

它显示在文档中: https ://developer.android.com/guide/topics/text/spell-checker-framework

于 2019-02-09T07:38:55.037 回答