将文本粘贴到多个编辑框中

3

我有一组四个EditText视图,用于输入4位代码。每个视图都设置为最大长度为1,因为它们只包含一个数字。

现在我想允许用户复制这四位数字代码并直接粘贴到4个字段中。

我尝试使用以下方法检测粘贴事件:

@Override
    public boolean onTextContextMenuItem(int id) {
        boolean consumed = super.onTextContextMenuItem(id);
        switch (id){
            case android.R.id.cut:
                onTextCut();
                break;
            case android.R.id.paste:
                onTextPaste();
                break;
            case android.R.id.copy:
                onTextCopy();
        }
        return consumed;
    }

就像这个问题中所提到的那样,但我无法在回调函数中返回已粘贴的文本。

我还尝试了以下代码:

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { }

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}

但是,当我把文本粘贴进去时,我只得到了一个字符,我猜测这是因为 maxLength 被设为了1。

我该如何实现期望的行为呢?

1个回答

1
也许这可以帮助你。
 private void pasteText() {
        ClipboardManager clipboardManager = (ClipboardManager)
                getSystemService(Context.CLIPBOARD_SERVICE);

        if(clipboardManager.hasPrimaryClip()) {
            ClipData.Item item = clipboardManager.getPrimaryClip().getItemAt(0);

            CharSequence ptext = item.getText();
            for(int i = 0 ; i <= ptext.length() ; i++){
    // 4 cases and paste to 4 edittexts
    }
        }
    }

嗨@keser,你能再解释一下吗? - Abbas Jafari
@abbasjafary 这是一个非常老的问题,我已经很久没有开发安卓了。但我会尽力帮助你。你的问题是什么? - keser
谢谢您的回复,我有六个编辑文本框用于输入六位代码,我希望当用户复制并粘贴六位代码时,六个编辑文本框会自动填充。我希望您能理解我的意思。 - Abbas Jafari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接