android - 如何从软键盘中候选 View 上显示的 Assets 中读取文本字典

标签 android dictionary keyboard android-softkeyboard

我正在 updateCandidates() 中开发软键盘。我的问题是如何在 Composing 有一些单词时检索每个文本,然后在候选 View 中加载我的字典。

图片:

enter image description here

如何加载单词。

图片

enter image description here 像这样

感谢您与我分享有关它的任何信息。

最佳答案

解决方案

你可以像这样制作方法:

public ArrayList<String> readFile() {
        ArrayList<String> list = new ArrayList<String>();

        try {
            AssetManager am = getAssets();
            InputStream data = am.open("data.txt");
            InputStreamReader dataInputStream = new InputStreamReader(data);
            BufferedReader reader = new BufferedReader(dataInputStream);
            String each = null;
            while ((each = reader.readLine()) != null) {
                list.add(each);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;

    }

编辑软键盘

pickSuggestionManually()

public void pickSuggestionManually(int index) {
        if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
            CompletionInfo ci = mCompletions[index];
            getCurrentInputConnection().commitCompletion(ci);
            if (mCandidateView != null) {
                mCandidateView.clear();
            }
            updateShiftKeyState(getCurrentInputEditorInfo());
        } else if (mComposing.length() > 0) {
            // If we were generating candidate suggestions for the current
            // text, we would commit one of them here. But for this sample,
            // we will just commit the current text.
            mComposing.setLength(index);
            mComposing = new StringBuilder(mCandidateList.get(index) + " ");
            commitTyped(getCurrentInputConnection());
        }
    }

这里重要updateCandidates()

    ArrayList<String> listData = readFile();

    System.out.println("Sonu Kumar: " + listData);

    if (!mCompletionOn) {
        if (mComposing.length() > 0) {
            ArrayList<String> list = new ArrayList<String>();
            // list.add(mComposing.toString());

            for (int j = 0; j < listData.size(); j++) {
                String str = mComposing.toString().toLowerCase();
                if (listData.get(j).startsWith(str)) {
                    list.add(listData.get(j));
                }
            }
            mCandidateList = list;
            setSuggestions(list, true, true);
        } else {
            setSuggestions(null, false, false);
        }
    }

感谢

关于android - 如何从软键盘中候选 View 上显示的 Assets 中读取文本字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035399/

相关文章:

python - 如何通过按某个键盘键来关闭 tkinter 主循环

android - Android 上的假鼠标和键盘输入

android - 如何在移动浏览器(Android 和 iOS)上停止自动更正 PWA 的输入文本?

android - 使用 setcontentview 两次

java - 反射、哈希表或性能方法?

android - 如何在水平 ListView 中获取动态数据

c++ - 从另一个 cpp 文件更改结构内、映射键内的变量

c# - 如何在 C# 中将字典(以数组作为值)转换为列表

android - 将数据从 php 发送到 android 集

android - 在android中相互叠加绘制图像