如何在软键盘的候选视图中显示来自assests的文本字典

时间:2015-10-09 10:04:11

标签: android dictionary keyboard android-softkeyboard

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

图片:

enter image description here

如何加载单词。

图片

enter image description here 像这样

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

2 个答案:

答案 0 :(得分:2)

<强>解决方案

你可以制作这样的方法:

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);
        }
    }

谢谢

答案 1 :(得分:1)

要在候选视图中显示建议单词,请修改SoftKeyboard.java文件的updateCandidates()方法:

 ArrayList<String> list = new ArrayList<String>();
    updateCandidate(){ 
    if (!mCompletionOn) {
      list.clear();
        if (mComposing.length() > 0) {       

            String mStr= mComposing.toString().toLowerCase();

            for (int i = 0; i < Dictionary.data.length; i++) {
                 String str = Dictionary.data[i];
                if (str.startsWith(mStr)) {
                    list.add(str);
                }
            }       
            setSuggestions(list, true, true);
        } else {
            setSuggestions(null, false, false);
        }
     }

    }

要将候选视图中选取的单词输入到输入文本,请在SoftKeyboard示例项目中修改以下方法。

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) {

       mComposing.setLength(index);
        mComposing = new StringBuilder(mCandidateList.get(index) + " ");
        commitTyped(getCurrentInputConnection());

    }
}