Android - Spelling Checker Tutorial

<p >The Android platform offers a spelling checker framework that lets you implement and access spell checking in your application.</p> <p >In order to use spelling checker , you need to implement<b >SpellCheckerSessionListener</b>interface and override its methods. Its syntax is given below:</p> <pre class="prettyprint notranslate" > 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 } } </pre> <p >Next thing you need to do is to create an object of<b >SpellCheckerSession</b>class. This object can be instantiated by calling<b >newSpellCheckerSession</b>method of TextServicesManager class. This class handles interaction between application and text services. You need to request system service to instantiate it. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate" > private SpellCheckerSession mScs; final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); mScs = tsm.newSpellCheckerSession(null, null, this, true); </pre> <p >The last thing you need to do is to call<b >getSuggestions</b>method to get suggestion for any text , you want. The suggestions will be passed onto the<b >onGetSuggestions</b>method from where you can do whatever you want.</p> <pre class="prettyprint notranslate" > mScs.getSuggestions(new TextInfo(editText1.getText().toString()), 3); </pre> <p >This method takes two parameters. First parameter is the string in the form of Text Info object , and second parameter is the cookie number used to distinguish suggestions.</p> <p >Apart from the the methods , there are other methods provided by the<b >SpellCheckerSession</b>class for better handling suggestions. These methods are listed below:</p> <table class="table table-bordered" > <tbody > <tr > <th >Sr.No</th> <th >Method &amp; description</th> </tr> <tr > <td >1</td> <td ><b >cancel()</b> <p >Cancel pending and running spell check tasks</p> </td> </tr> <tr > <td >2</td> <td ><b >close()</b> <p >Finish this session and allow TextServicesManagerService to disconnect the bound spell checker</p> </td> </tr> <tr > <td >3</td> <td ><b >getSentenceSuggestions(TextInfo[] textInfos, int suggestionsLimit)</b> <p >Get suggestions from the specified sentences</p> </td> </tr> <tr > <td >4</td> <td ><b >getSpellChecker()</b> <p >Get the spell checker service info this spell checker session has.</p> </td> </tr> <tr > <td >5</td> <td ><b >isSessionDisconnected()</b> <p >True if the connection to a text service of this session is disconnected and not alive.</p> </td> </tr> </tbody> </table> <h2 >Example</h2> <p >Here is an example demonstrating the use of Spell Checker. It creates a basic spell checking application that allows you to write text and get suggestions.</p> <p >To experiment with this example , you can run this on an actual device or in an emulator.</p> <table class="table table-bordered" > <tbody > <tr > <th >Steps</th> <th >Description</th> </tr> <tr > <td >1</td> <td >You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.</td> </tr> <tr > <td >2</td> <td >Modify src/ file to add necessary code.</td> </tr> <tr > <td >3</td> <td >Modify the res/layout/main to add respective XML components</td> </tr> <tr > <td >4</td> <td >Run the application and choose a running android device and install the application on it and verify the results</td> </tr> </tbody> </table> <p >Following is the content of the modified main activity file<b >src/</b>.</p> <pre class="prettyprint notranslate" > package com.example.sairamkrishna.myapplication; import; import; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.MenuItem; 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(; tv1=(TextView)findViewById(; ed1=(EditText)findViewById(; 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 &lt; arg0.length; ++i) { // Returned suggestions are contained in SuggestionsInfo final int len = arg0[i].getSuggestionsCount(); sb.append(&#39;\n&#39;); for (int j = 0; j &lt; len; ++j) { sb.append(&quot;,&quot; + arg0[i].getSuggestionAt(j)); } sb.append(&quot; (&quot; + len + &quot;)&quot;); } runOnUiThread(new Runnable() { public void run() { tv1.append(sb.toString()); } }); } @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) { // TODO Auto-generated method stub } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == { return true; } return super.onOptionsItemSelected(item); } } </pre> <p >Following is the modified content of the xml<b >res/layout/main.xml</b>.</p> <pre class="prettyprint notranslate" > &lt;RelativeLayout xmlns:android=&quot;; xmlns:tools=&quot;; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot; android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot; android:paddingTop=&quot;@dimen/activity_vertical_margin&quot; android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot; tools:context=&quot;.MainActivity&quot;&gt; &lt;TextView android:text=&quot;Spell checker &quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:id=&quot;@+id/textview&quot; android:textSize=&quot;35dp&quot; android:layout_alignParentTop=&quot;true&quot; android:layout_centerHorizontal=&quot;true&quot; /&gt; &lt;TextView android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;Tutorials point&quot; android:id=&quot;@+id/textView&quot; android:layout_below=&quot;@+id/textview&quot; android:layout_centerHorizontal=&quot;true&quot; android:textColor=&quot;#ff7aff24&quot; android:textSize=&quot;35dp&quot; /&gt; &lt;Button android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;Suggestions&quot; android:id=&quot;@+id/button&quot; android:layout_alignParentBottom=&quot;true&quot; android:layout_centerHorizontal=&quot;true&quot; /&gt; &lt;EditText android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:id=&quot;@+id/editText&quot; android:hint=&quot;Enter Text&quot; android:layout_above=&quot;@+id/button&quot; android:layout_marginBottom=&quot;56dp&quot; android:focusable=&quot;true&quot; android:textColorHighlight=&quot;#ff7eff15&quot; android:textColorHint=&quot;#ffff25e6&quot; android:layout_alignRight=&quot;@+id/textview&quot; android:layout_alignEnd=&quot;@+id/textview&quot; android:layout_alignLeft=&quot;@+id/textview&quot; android:layout_alignStart=&quot;@+id/textview&quot; /&gt; &lt;ImageView android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:id=&quot;@+id/imageView&quot; android:src=&quot;@drawable/abc&quot; android:layout_below=&quot;@+id/textView&quot; android:layout_centerHorizontal=&quot;true&quot; /&gt; &lt;TextView android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;Suggestions&quot; android:id=&quot;@+id/textView3&quot; android:textSize=&quot;25sp&quot; android:layout_below=&quot;@+id/imageView&quot; /&gt; &lt;/RelativeLayout&gt; </pre> <p >Following is the content of the<b >res/values/string.xml</b>.</p> <pre class="prettyprint notranslate" > &lt;resources&gt; &lt;string name=&quot;app_name&quot;&gt;My Application&lt;/string&gt; &lt;string name=&quot;hello_world&quot;&gt;Hello world!&lt;/string&gt; &lt;string name=&quot;action_settings&quot;&gt;Settings&lt;/string&gt; &lt;/resources&gt; </pre> <p >Following is the content of<b >AndroidManifest.xml</b>file.</p> <pre class="prettyprint notranslate" > &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;manifest xmlns:android=&quot;; package=&quot;com.example.sairamkrishna.myapplication&quot; &gt; &lt;application android:allowBackup=&quot;true&quot; android:icon=&quot;@mipmap/ic_launcher&quot; android:label=&quot;@string/app_name&quot; android:theme=&quot;@style/AppTheme&quot; &gt; &lt;activity android:name=&quot;.MainActivity&quot; android:label=&quot;@string/app_name&quot; &gt; &lt;intent-filter&gt; &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt; &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </pre> <p >Let&#39;s try to run ourr application we just modified. I assume you had created your<b >AVD</b>while doing environment setup. To run the app from Android studio, open one of your project&#39;s activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="" />icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window &minus;</p> <p><img alt="Android Spell Checker Tutorial" src="" /></p> <p >Now what you need to do is to enter any text in the field. For example , i have entered some text. Press the suggestions button. The following notification would appear in you AVD along with suggestions &minus;</p> <p><img alt="Android Spell Checker Tutorial" src="" /></p> <p >Now change the text and press the button again, like i did. And this is what comes on screen.</p> <p><img alt="Android Spell Checker Tutorial" src="" /></p> <div></div> <p><script language="javascript"><br /> function CheckAll(form,name)<br /> {<br /> for (var i=0;i<form.elements.length;i++)<br /> {<br /> var e = form.elements[i];<br /> if ( == name)<br /> e.checked = form.chkall.checked;<br /> }<br /> }<br /> function checklistform()<br /> {<br /> var do_action;<br /> var obj = document.getElementsByName("do_action"); //这个是以标签的name来取控件 <br /> for(i=0; i<obj.length;i++) { <br /> if(obj[i].checked){ <br /> do_action=obj[i].value; <br /> }<br /> } <br /> //if(do_action=="undefined"){alert("请选择");return false;};<br /> if(do_action=="del")<br /> {<br /> if(!confirm("确定删除吗?"))<br /> {<br /> return false;<br /> }<br /> }<br /> }<br /> </script></p>


  1. android


  1. 2021-12 (131)
RangeMem:219.66 KB
返回顶部 留言