拼写检查工具是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);

该方法有两个参数。第一个参数是Text Info对象形式的字符串,第二个参数是用于区分建议的cookie号。

除了这些方法之外, SpellCheckerSession 类还提供了其他方法来提供更好的处理建议。这些方法如下 -

序号

Method & description

1

cancel()

取消待处理和运行拼写检查任务

2

close()

完成此会话并允许TextServicesManagerService断开绑定的拼写检查程序

3

getSentenceSuggestions(TextInfo[] textInfos, int suggestionsLimit)

从指定的句子中获取建议

4

getSpellChecker()

获取此拼写检查程序会话的拼写检查服务信息。

5

isSessionDisconnected()

如果与此会话的文本服务的连接已断开且未处于活动状态,则为True。

这是一个演示拼写检查器使用的示例。它创建了一个基本的拼写检查应用程序,允许您编写文本并获取建议。

要试验此示例,您可以在实际设备或模拟器中运行此示例。

序号

描述

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 的修改内容。

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">

android:layout_height="wrap_content"

android:id="@+id/textview"

android:textSize="35dp"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true" />

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" />

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" />

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" />

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" />

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" />

以下是 res / values / string.xml 的内容 。

My Application

以下是 AndroidManifest.xml 文件的内容。

package="com.example.sairamkrishna.myapplication" >

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name" >

让我们尝试运行我们刚修改过的应用程序。我假设您在进行环境设置时创建了 AVD

。要从Android工作室运行应用程序,请打开项目的某个活动文件,然后单击

76338ddfc058aba988fb4a0c0612fde3.png工具栏中的“运行”

图标。Android工作室在您的AVD上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 -

f16d65be43837349a9b725f1da3ccb18.png

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

8e75b01ba6bc5472596887fa0cecd8c2.png

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

ee17d4b886129eb77946f5ca19e5e6d0.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值