Android 基础知识

Android 主页 Android 概述 Android 下载安装和开发环境配置 Android Studio IDE Android 架构 Android 应用程序组件 Android Hello World 示例 Android 资源 Android 活动 Android 服务 Android 广播接收器 Android 内容提供者 Android 片段 Android Intents/Filters

Android 用户界面

Android UI 布局 Android UI 控件 Android 事件处理 Android 样式和主题 Android 自定义组件

Android 高级概念

Android 拖放 Android 通知 Android 基于位置的服务 Android 发送电子邮件 Android 发送短信 Android 拨打电话 Android 发布应用程序

Android 实用示例

Android 警报对话框 Android 动画 Android 音频捕捉 Android 音频管理器 Android 自动完成 Android 最佳实践 Android 蓝牙 Android 相机 Android 剪贴板 Android 自定义字体 Android 数据备份 Android 开发者工具 Android 模拟器 Android Facebook 集成 Android 手势 Android 谷歌地图 Android 图像效果 Android 图像切换 Android 内部存储 Android JetPlayer Android JSON 解析器 Android Linkedin 集成 Android 旋转加载器 Android 本地化 Android 登录应用 Android 媒体播放器 Android 多点触控 Android 导航 Android 网络连接 Android NFC 指南 Android PHP/MySQL Android 进度圈 Android 进度条 Android 推送通知 Android 渲染脚本 Android RSS 阅读器 Android 屏幕投射 Android SDK 管理器 Android 传感器 Android 会话管理 Android 共享首选项 Android SIP 协议 Android 拼写检查器 Android SQLite 数据库 Android 支持库 Android 测试 Android 文字转语音 Android TextureView Android Twitter 集成 Android UI 设计 Android UI 模式 Android UI 测试 Android WebView 布局 Android Wi-Fi Android Widgets Android XML 解析器

Android 其他

Android 面试问题 Android 有用的资源 Android 测验


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 Eclipse 运行图标 图标。 Android Studio 在您的 AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下 Emulator 窗口 −

Android 拼写检查教程

现在您需要做的是在该字段中输入任何文本。 例如,我输入了一些文本。 按建议按钮。 以下通知将与建议一起出现在您的 AVD 中 −

Android 拼写检查教程

现在更改文本并再次按下按钮,就像我一样。 这就是屏幕上出现的内容。

Android 拼写检查教程