我正在嘗試執行拼寫檢查服務as described here,調用SampleSpellCheckerService
,但它似乎是教程不完整,並且它的源代碼似乎不可用。Android - 如何從我的拼寫檢查服務中獲取會話?
我與如何從我的拼寫檢查服務在我的活動setSuggestionsFor()
方法獲得一個會話中掙扎,因爲突出的位置:
public class SpellCheckerSettingsActivity extends AppCompatActivity implements SpellCheckerSession.SpellCheckerSessionListener {
private static final String LOG_TAG = SpellCheckerSettingsActivity.class.getSimpleName();
private TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spell_checker_settings);
final EditText editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fetchSuggestionsFor(editText.getText().toString());
}
});
startService(new Intent(this, SampleSpellCheckerService.class));
}
private void fetchSuggestionsFor(String input){
Log.d(LOG_TAG, "fetchSuggestionsFor(\"" + input + "\")");
/***************************************************
*
* This line is invalid. What do I replace it with?
*
***************************************************/
SpellCheckerSession session = SampleSpellCheckerService.getSession();
TextInfo[] textInfos = new TextInfo[]{ new TextInfo(input) };
int suggestionsLimit = 5;
session.getSentenceSuggestions(textInfos, suggestionsLimit);
}
@Override
public void onGetSuggestions(SuggestionsInfo[] results) {
Log.d(LOG_TAG, "onGetSuggestions(" + results + ")");
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("Suggestions obtained (TODO - get from results[])");
}
});
}
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
Log.d(LOG_TAG, "onGetSentenceSuggestions(" + results + ")");
if (results != null) {
final StringBuffer sb = new StringBuffer("");
for (SentenceSuggestionsInfo result : results) {
int n = result.getSuggestionsCount();
for (int i = 0; i < n; i++) {
int m = result.getSuggestionsInfoAt(i).getSuggestionsCount();
for (int k = 0; k < m; k++) {
sb.append(result.getSuggestionsInfoAt(i).getSuggestionAt(k))
.append("\n");
}
sb.append("\n");
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(sb.toString());
}
});
}
}
@Override
public void onDestroy() {
stopService(new Intent(this, SampleSpellCheckerService.class));
super.onDestroy();
}
}
那麼,什麼是擺脫SampleSpellCheckerService
會話的正確方法?
爲了完整起見,這裏是我的拼寫檢查服務類:
public class SampleSpellCheckerService extends SpellCheckerService {
public static final String LOG_TAG = SampleSpellCheckerService.class.getSimpleName();
public SampleSpellCheckerService() {
Log.d(LOG_TAG, "SampleSpellCheckerService");
}
@Override
public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "SampleSpellCheckerService.onCreate");
}
@Override
public Session createSession() {
Log.d(LOG_TAG, "createSession");
return new AndroidSpellCheckerSession();
}
private static class AndroidSpellCheckerSession extends SpellCheckerService.Session {
@Override
public void onCreate() {
Log.d(LOG_TAG, "AndroidSpellCheckerSession.onCreate");
}
@Override
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) {
Log.d(LOG_TAG, "onGetSentenceSuggestionsMultiple");
SentenceSuggestionsInfo[] suggestionsInfos = null;
//suggestionsInfo = new SuggestionsInfo();
//... // look up suggestions for TextInfo
return suggestionsInfos;
}
@Override
public SuggestionsInfo onGetSuggestions(TextInfo textInfo, int suggestionsLimit) {
Log.d(LOG_TAG, "onGetSuggestions");
SuggestionsInfo suggestionsInfo = null;
//suggestionsInfo = new SuggestionsInfo();
//... // look up suggestions for TextInfo
return suggestionsInfo;
}
@Override
public void onCancel() {
Log.d(LOG_TAG, "onCancel");
}
}
}
這裏是我的清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<permission android:name="android.permission.BIND_TEXT_SERVICE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name="com.example.SampleSpellCheckerService"
android:label="@string/app_name"
android:enabled="true"
android:permission="android.permission.BIND_TEXT_SERVICE">
<intent-filter>
<action android:name="android.service.textservice.SpellCheckerService" />
</intent-filter>
<meta-data
android:name="android.view.textservice.scs"
android:resource="@xml/spellchecker" />
</service>
<activity android:name="com.example.SpellCheckerSettingsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
這裏是我的spellchecker.xml:
<?xml version="1.0" encoding="utf-8"?>
<spell-checker
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/spellchecker_name"
android:settingsActivity="com.example.SpellCheckerSettingsActivity">
<subtype
android:label="@string/subtype_generic"
android:subtypeLocale="en" />
/>
<subtype
android:label="@string/subtype_generic"
android:subtypeLocale="en_GB" />
/>
</spell-checker>
NB - 我正在使用三星設備進行測試。
感謝: 如果你不需要爲您服務的任何設置,您可以從XML刪除XML屬性。期待用戶使用他們的設置來解決問題對我來說不是一個可行的解決方案。你能推薦一個更好的方式來實現我的自定義拼寫檢查器在SearchView上嗎?要求是,如果用戶輸入的產品名稱不正確(例如* Badli拼寫產品*),他們將被提供實際產品(例如,我的*拼寫錯誤*產品)。我是否需要從頭開始編寫此功能? (或者,也許有一個免費的,可靠的,基於Web的API,我可以使用?) –
恕我直言:整個拼寫檢查API只是某種步驟的孩子。在我的HTC既沒有谷歌鍵盤也沒有HTC感覺使用自定義拼寫檢查器,但都使用自己的 - 這和另一個應用程序,想要使用你的拼寫檢查器必須實現一個特定的界面使這種用戶無用。對於應用程序特定的拼寫檢查(就像你想要的那樣),我會放下整個拼寫檢查API,然後用textwatcher –
謝謝,但TextWatcher如何幫助重新拼寫檢查? –