拼写检查器框架未返回建议

时间:2019-02-09 07:33:58

标签: android-studio android-edittext android-spellcheck

我正在尝试对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
}

}

谢谢。

1 个答案:

答案 0 :(得分:0)

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

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