前提條件:確保你有適當的UserDictionary在的話
Settings
- >Language & Input
- >Personal dictionary
。
樣品SQL查詢,其搜索包含在用戶字典SO
字和它的等效的Android代碼示例。注意?
的用法被替換爲args
。
SQL查詢:
SELECT UserDictionary.Words._ID, UserDictionary.Words.WORD FROM UserDictionary.Words.CONTENT_URI WHERE UserDictionary.Words.WORD LIKE "%SO%
等效代碼:
String[] columns = {UserDictionary.Words._ID, UserDictionary.Words.WORD};
String condition = UserDictionary.Words.WORD + " LIKE ? ";
// ? in condition will be replaced by `args` in order.
String[] args = {"%SO%"};
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, columns, condition, args, null);
//Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null); - get all words from dictionary
if (cursor != null) {
int index = cursor.getColumnIndex(UserDictionary.Words.WORD);
//iterate over all words found
while (cursor.moveToNext()) {
//gets the value from the column.
String word = cursor.getString(index);
Log.i(TAG, "Word found: " + word);
}
}
權限在AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
如果「測試」是不是在UserDictionary,那麼就會使光標空。 – Suragch