java—如何在“edittext”中键入最后一个单词?

cgvd09ve  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(302)

我正在建立一个原型社交网络,我允许我的用户用@标记另一个用户,我使用自动完成文本视图来显示用户@搜索的对话框,但我需要知道用户何时键入“@”以及EditText中的字母。我找到了这个答案,这是我需要的,但我不想只有一个字符。我要整个词在我的数据库里搜索。例如,用户在粘贴文本的中间键入“@jordan”。我要去拿“@”和“@jordan”。我该怎么做?
下面是我的代码示例

private final TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(s) && start < s.length()) {

            if (!mentionAdapter.isEmpty()) {
                mentionAdapter.clear();
            }

            String lastWord = s.toString().substring(s.toString().lastIndexOf(" ") + 1);
            if (lastWord != null){
                if (lastWord.length() != 0) {

                    switch (lastWord.charAt(0)) {
                        case '#':
                            if (getAdapter() != hashtagAdapter) {
                                setAdapter(hashtagAdapter);
                            }
                            break;
                        case '@':

                            if (getAdapter() != mentionAdapter) {
                                setAdapter(mentionAdapter);
                            }
                            break;
                    }
                }
        }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
zpf6vheq

zpf6vheq1#

我的目标是你在androidjava上工作,所以这里是你问题的答案

EditText editText = (EditText)findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    try {
    String capturedString = getText(s);
    } catch (Exception e) {
    e.printStackTrace();
    }
} 
});

每当您在空格键“@jordan”上添加制表符时,此函数都会起作用,“@”之后的字符串和“”之前的字符串意味着您将获得字符串形式的“jordan”

public String getText(String s) {
    String startChar = "#";
    String endChar   = " ";
    String output = getStringBetweenTwoChars(s, startChar, endChar);
    System.out.println(output);
}

下面是getstringbetweentwochars函数

public String getStringBetweenTwoChars(String input, String startChar, String endChar) {
try {
    int start = input.indexOf(startChar);
    if (start != -1) {
        int end = input.indexOf(endChar, start + startChar.length());
        if (end != -1) {
            return input.substring(start + startChar.length(), end);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
return input;
}
7jmck4yq

7jmck4yq2#

你可以通过下面的代码来实现,如果你想@java从字符串你好这是@java最好的编程语言

edt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(final CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String str = editable.toString();
            String seperator = "@";
            int seoPos = str.indexOf(seperator);
            Pattern pattern = Pattern.compile("\\s");
            Matcher matcher = pattern.matcher(str);
            boolean found = matcher.find();

                if (seoPos != -1 && !found){
                    Log.d("TextChanged0","current Char  "+str.substring(seoPos-1+seperator.length()));
                }

        }
    });

相关问题