0

我有一个简单的活动,用于SpellCheckerSession建议单词的相似匹配。但如果键盘显示拼写检查器不起作用。也许是因为键盘也在使用拼写检查器。

我的简化活动是:

    public class TestActivity extends AppCompatActivity  implements  SpellCheckerSession.SpellCheckerSessionListener{

    private SpellCheckerSession spellChecker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        test();

    }

    void test() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                spellChecker.getSentenceSuggestions(new TextInfo[] {new TextInfo("bool")}, 4);
                test();

            }
        }, 2000);
    }

    @Override
    public void onGetSuggestions(SuggestionsInfo[] results) {}

    @Override
    public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {

        Log.i("tag", "results size: " + results.length);
        for (SentenceSuggestionsInfo result : results) {
            final int len = result.getSuggestionsCount();

            for (int j = 0; j < len; ++j) {
                int a = result.getSuggestionsInfoAt(j).getSuggestionsCount();
                Log.i("tag", "getSuggestionsCount : " + a);

                for (int i = 0; i < a; ++i) {
                    Log.i("tag", "Suggestion: " + result.getSuggestionsInfoAt(j).getSuggestionAt(i));
                }
            }
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        final TextServicesManager tsm = (TextServicesManager)
                getApplicationContext().getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
        if (tsm != null) {
            spellChecker = tsm.newSpellCheckerSession(null, Locale.US, this, true);

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (spellChecker != null) {
            spellChecker.close();
        }
    }
}

在这个活动中,我getSentenceSuggestions每 2 秒调用一次以测试它是否工作。在我的活动布局中,我有一个 EditText。午餐后应用程序 spellChecker 工作并在 logcat 中显示建议,但如果单击 editText 以显示 keyboaar,则 spellChecker 不起作用并getSuggestionsCount在 logcat 中显示 -1。如果隐藏键盘一切正常。

4

1 回答 1

0

通过添加android:inputType="textNoSuggestions|textVisiblePassword"解决EditText了这个问题。

另一种方法是:

EditText editText = findViewById(R.id.edit_text);
    editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

如果您使用像搜索栏这样的库,并且不访问 EditText 视图,也许这对您有用:

EditText view = (EditText) getCurrentFocus();
            if (view != null) {
                view.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
            }
于 2019-11-11T06:35:26.813 回答