android - 如何将新词添加到 Android 用户词典,以便它们在输入时出现在候选人 View 中

标签 android android-webview android-softkeyboard search-suggestion android-userdictionary

我在网上搜索和研究了这个问题,但没有得到明确的答案。我已经使用内容提供者将单词添加到用户词典中作为 android documentation说明单词已添加,但在那之后当我在键盘上键入时,我没有看到候选人 View 中的建议中出现该单词,因为我们下次点击的其他单词会出现在那里。我真的很感激这个问题的完整答案,因为很多人在网上提问但没有得到回答。 这个我试过了

    Uri mNewUri;
    // Defines an object to contain the new values to insert
    ContentValues mNewValues = new ContentValues();

     // Sets the values of each column and inserts the word. The arguments to the "put"
     // method are "column name" and "value"

    mNewValues.put(UserDictionary.Words.APP_ID, "example.user");
    mNewValues.put(UserDictionary.Words.LOCALE, "en_US");
    mNewValues.put(UserDictionary.Words.WORD, "Qasim");
    mNewValues.put(UserDictionary.Words.FREQUENCY, "100");

    mNewUri = getContentResolver().insert(
        UserDictionary.Words.CONTENT_URI,   // the user dictionary content URI
        mNewValues                          // the values to insert
    );


    Uri dic = UserDictionary.Words.CONTENT_URI;
    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(dic, null, null, null, null);
 //here i retrieve all the words stored into my dictionary
    while (cursor.moveToNext()){
       String word = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD));
       int id = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words._ID));
       String app = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.APP_ID));
       int frequency = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words.FREQUENCY));
       String locale = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.LOCALE));
       Log.i("", "word: "+word+"\nId: "+id+"\nAppID: "+app+"\nfrequency: "+frequency+"\nLocale:"+locale);
    }

如果有人帮助我,我将不胜感激

最佳答案

如果您使用的键盘读取用户词典,您应该能够按照文档所述使用 resolver.insert 向词典中添加单词。

我使用默认键盘手动添加了一些单词。使用 resolver.query,新词列在输出中。键盘正在向字典中添加新词。

之后,我使用 resolver.insert 向字典 (Quasimodo) 添加了一个单词。该词已添加到字典中(它出现在 resolver.query 输出中)并且它还作为建议出现在键盘上。

如果我切换到另一个键盘(例如 Swiftkey),这些词不会用于预测。也许您正在使用一些不同的键盘。

我的完整代码是:

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get the TextView which will be populated with the Dictionary ContentProvider data.
        TextView dictTextView = (TextView) findViewById(R.id.dictionary_text_view);
        // Get the ContentResolver which will send a message to the ContentProvider
        ContentResolver resolver = getContentResolver();
        // Put a new word on the dictionary
        final Locale locale;
        locale = Locale.getDefault();
        final int COLUMN_COUNT = 3;
        ContentValues values = new ContentValues(COLUMN_COUNT);
        values.put(Words.WORD, "Quasimodo");
        values.put(Words.FREQUENCY, 250);
        values.put(Words.LOCALE, locale.toString());
        Uri result = resolver.insert(UserDictionary.Words.CONTENT_URI, values);

        // Get a Cursor containing all of the rows in the Words table
        // The word "Quasimodo" inserted will be shown
        Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);
        // Surround the cursor in a try statement so that the finally block will eventually execute
        try {
            dictTextView.setText("UserDictionary contains " + cursor.getCount() + " words\n");
            dictTextView.append("COLUMNS: " + Words._ID  + " - " + Words.FREQUENCY +
                    " - " + Words.WORD);
            int idColumn = cursor.getColumnIndex(UserDictionary.Words._ID);
            int frequencyColumn = cursor.getColumnIndex(UserDictionary.Words.FREQUENCY);
            int wordColumn = cursor.getColumnIndex(UserDictionary.Words.WORD);
            while (cursor.moveToNext()) {
                int id = cursor.getInt(idColumn);
                int frequency = cursor.getInt(frequencyColumn);
                String word = cursor.getString(wordColumn);
                dictTextView.append(("\n" + id + " - " + frequency + " - " + word));
            }
        } finally {
            cursor.close();
        }
    }
}

之后我的键盘会显示单词 insert 作为建议。

enter image description here

关于android - 如何将新词添加到 Android 用户词典,以便它们在输入时出现在候选人 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25910733/

相关文章:

android - 在没有连接的情况下查询应用内产品库存

android - Android 中 "Java class extends" "AppcompatActivity"与 "Activity"与 "ActionBar"之间的区别?

android - WebView 不会播放嵌入的 Flash 视频

java - saveWebArchive() 不适用于 Android 4.0.3

android - 链接 APK 以在我的网站上自动安装

android - Toast onItemClickListener 使用字符串作为消息?

android - Flutter 应用程序仅在通过 APK 文件安装时停止。如何解决?

Android EditText,minus通过inputType ="number|numberSigned|numberDecimal"转换成点事件

android - 弹出窗口打开时软键盘未打开

Android:setOnItemClickListener 不适用于软键盘中的 ListView