如何创建英语拼写检查并相应地获取建议?

时间:2018-03-20 07:09:59

标签: android spell-checking spelling

我想开发一个应用程序,当用户在带有红色标记的编辑文本中写错拼写时检查拼写并相应地给出建议。我尝试了这段代码但没有使用设备此代码适用于模拟器但不是在任何手机上工作。请建议我一些参考

请帮助。提前致谢

MainActivity.java

 public class MainActivity extends AppCompatActivity implements 
   SpellCheckerSession.SpellCheckerSessionListener
{
 Button b1;
 TextView tv1;
 EditText ed1;
 private SpellCheckerSession mScs;

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

    b1=(Button)findViewById(R.id.button);
    //tv1=(TextView)findViewById(R.id.textView3);

    ed1=(EditText)findViewById(R.id.editText);

}
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();

    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));
        }


        sb.append(" (" + len + ")");
    }
    mScs.getSuggestions(new TextInfo(ed1.getText().toString()), 3);

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

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

0 个答案:

没有答案