Android平台提供拼写检查框架,可让您在应用程序中实现和访问拼写检查.
为了使用拼写检查器,您需要实现 SpellCheckerSessionListener 接口并覆盖其方法.其语法在下面和下面给出;
public class HelloSpellCheckerActivity extends Activity implements SpellCheckerSessionListener { @Override public void onGetSuggestions(final SuggestionsInfo[] arg0) { // TODO Auto-generated method stub } @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) { // TODO Auto-generated method stub }}
接下来你需要做的是创建一个对象 SpellCheckerSession 类.可以通过调用TextServicesManager类的 newSpellCheckerSession 方法来实例化此对象.此类处理应用程序和文本服务之间的交互.您需要请求系统服务来实例化它.它的语法在下面和下面给出;
private SpellCheckerSession mScs;final TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);mScs = tsm.newSpellCheckerSession(null, null, this, true);
您需要做的最后一件事是调用 getSuggestions 方法来获取您想要的任何文本的建议.建议将传递到 onGetSuggestions 方法,您可以从中执行任何操作.
mScs.getSuggestions(new TextInfo(editText1.getText().toString()), 3);
此方法有两个参数.第一个参数是Text Info对象形式的字符串,第二个参数是用于区分建议的cookie号.
除了方法之外,还有其他方法提供的 SpellCheckerSession 课程,以获得更好的处理建议.这些方法列在下面 :
Sr.No | 方法&描述 |
---|---|
1 | cancel() 取消待处理和运行拼写检查任务 |
2 | close() 完成此会话并允许TextServicesManagerService断开绑定的拼写检查程序 |
3 | getSentenceSuggestions(TextInfo [] textInfos,int suggestionsLimit) 从指定的句子中获取建议 |
4 | getSpellChecker() 获取此拼写检查程序会话的拼写检查服务信息. |
5 | isSessionDisconnected() 如果与文本服务的连接为真,则为真此会话已断开连接且未处于活动状态. |
示例
这是一个展示Spe使用的例子ll Checker.它创建了一个基本的拼写检查应用程序,允许您编写文本并获取建议.
要试验此示例,您可以在实际设备或模拟器中运行它.
步骤 | 描述 |
---|---|
1 | 您将使用Android工作室创建Android com.example.sairamkrishna.myapplication包下的应用程序. |
2 | 修改src/MainActivity.java文件以添加必要的代码. |
3 | 修改res/layout/main以添加相应的XML组件 |
4 | 运行应用程序并选择正在运行的Android设备并在其上安装应用程序并验证结果 |
以下是内容已修改的主活动文件 src/MainActivity.java .
package com.example.sairamkrishna.myapplication;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.textservice.TextInfo;import android.view.textservice.TextServicesManager;import android.widget.Button;import android.widget.EditText;import android.view.textservice.SentenceSuggestionsInfo;import android.view.textservice.SpellCheckerSession;import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;import android.view.textservice.SuggestionsInfo;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements 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); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), ed1.getText().toString(),Toast.LENGTH_SHORT).show(); mScs.getSuggestions(new TextInfo(ed1.getText().toString()), 3); } }); } 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 + ")"); } runOnUiThread(new Runnable() { public void run() { tv1.append(sb.toString()); } }); } @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) { // TODO Auto-generated method stub }}
以下是xml res/layout/main.xml 的修改内容.
在下面的代码中 abc 表示it1352的标识.com
以下是 res/values/string.xml 的内容.
My Application
以下是 AndroidManifest.xml 文件的内容.
让我们尝试运行我们刚修改过的应用程序.我假设您在进行环境设置时创建了 AVD .要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行 icon从工具栏中. Android工作室在您的AVD上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 :
现在您需要做的是在字段中输入任何文本.例如,我输入了一些文字.按建议按钮.以下通知将出现在您的AVD以及建议和减号;
现在改变文本并再次按下按钮,就像我一样.这就是屏幕上出现的内容.