Android - 拼写检查器
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);
这个方法有两个参数。 第一个参数是 TextInfo 对象形式的字符串,第二个参数是用来区分建议的 cookie 号。
除了方法之外,SpellCheckerSession 类还提供了其他方法来更好地处理建议。 下面列出了这些方法 −
| 序号 | 方法 & 描述 |
|---|---|
| 1 |
cancel() 取消挂起和正在运行的拼写检查任务 |
| 2 |
close() 完成此会话并允许 TextServicesManagerService 断开绑定的拼写检查器 |
| 3 |
getSentenceSuggestions(TextInfo[] textInfos, int suggestionsLimit) 从指定的句子中获取建议 |
| 4 |
getSpellChecker() 获取此拼写检查会话具有的拼写检查服务信息。 |
| 5 |
isSessionDisconnected() 如果与此会话的文本服务的连接已断开且不活跃,则为真。 |
示例
这是一个演示拼写检查器使用的示例。 它创建了一个基本的拼写检查应用程序,允许您编写文本并获得建议。
要试验此示例,您可以在实际设备或模拟器中运行它。
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用 Android Studio 在 com.example.sairamkrishna.myapplication 包下创建一个 Android 应用程序。 |
| 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表示tutorialspoint.com的标志
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="Spell checker " android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Suggestions"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter Text"
android:layout_above="@+id/button"
android:layout_marginBottom="56dp"
android:focusable="true"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
android:layout_alignRight="@+id/textview"
android:layout_alignEnd="@+id/textview"
android:layout_alignLeft="@+id/textview"
android:layout_alignStart="@+id/textview" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Suggestions"
android:id="@+id/textView3"
android:textSize="25sp"
android:layout_below="@+id/imageView" />
</RelativeLayout>
以下是 res/values/string.xml 的内容。
<resources> <string name="app_name">My Application</string> </resources>
以下是 AndroidManifest.xml 文件的内容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
让我们尝试运行我们刚刚修改的应用程序。 我假设您在进行环境设置时已经创建了 AVD。要从 Android Studio 运行应用程序,请打开项目的活动文件之一,然后单击工具栏中的 Run
图标。 Android Studio 在您的 AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下 Emulator 窗口 −
现在您需要做的是在该字段中输入任何文本。 例如,我输入了一些文本。 按建议按钮。 以下通知将与建议一起出现在您的 AVD 中 −
现在更改文本并再次按下按钮,就像我一样。 这就是屏幕上出现的内容。

