发布
社区首页 >问答首页 >拼写检查器框架不返回建议

拼写检查器框架不返回建议
EN

Stack Overflow用户
提问于 2019-02-09 15:33:58
回答 1查看 460关注 0票数 0

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

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

代码如下:

代码语言:javascript
代码运行次数:0
复制
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
}

}

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-09 15:38:55

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54604203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档