SpellChecker在Android中显示错误?

时间:2014-09-02 09:20:53

标签: android

我正在尝试使用 EditText 运行Android 拼写检查但是它没有运行显示一些错误,如下所示:

我的活动如下:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class HelloSpellCheckerActivity extends Activity implements SpellCheckerSessionListener {

   private static final int NOT_A_LENGTH = -1;
   private TextView mMainView;
   private SpellCheckerSession mScs;
   private EditText editText1;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      mMainView = (TextView)findViewById(R.id.main);
      editText1 = (EditText)findViewById(R.id.editText1);
   }

   @Override
   public void onResume() {
      super.onResume();
      final TextServicesManager tsm = (TextServicesManager) getSystemService(
      Context.TEXT_SERVICES_MANAGER_SERVICE);
      mScs = tsm.newSpellCheckerSession(null, null, this, true);         
   }

   @Override
   public void onPause() {
      super.onPause();
      if (mScs != null) {
         mScs.close();
      }
   }

   public void go(View view){
      Toast.makeText(getApplicationContext(), editText1.getText().toString(),    
      Toast.LENGTH_SHORT).show();
      mScs.getSuggestions(new TextInfo(editText1.getText().toString()), 3);

   }
   @Override
   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() {
         mMainView.append(sb.toString());
      }
   });

   }
   @Override
   public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
      // TODO Auto-generated method stub

   }
}

我正在上传错误的图片。请看一下,让我知道为什么会出现这个错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

你的清单有minSdkVersion为8,这不起作用。

您必须在清单文件中将minSdkVersion更改为14。

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

在API级别14中添加SpellCheckerSessionListener时,您的项目显示错误。

希望这有帮助。