【问题标题】:Paste into multiple edittext粘贴到多个edittext
【发布时间】:2019-03-17 11:03:30
【问题描述】:

我有一组四个 EditText 视图,用于输入 4 位代码。每一个都设置为 1 的 maxLength,因为它们包含其中一个数字。

现在我想让我的用户复制四位代码并将其直接粘贴到 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;
    }

类似于this question,但我无法在回调中返回粘贴的文本。

我也尝试过:

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

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

但是在粘贴的文本中我只得到 1 个字符,我猜是因为 maxLength 设置为 1。

我怎样才能达到预期的行为?

【问题讨论】:

    标签: android android-edittext


    【解决方案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,你能解释一下吗?
    • @abbasjafary 这是一个非常古老的问题,我已经很长时间没有开发安卓了。但我会尽力提供帮助。你有什么问题?
    • 感谢您的回复,我有六个用于输入六位代码的编辑文本,我希望当用户复制和过去六位代码时自动填充六个编辑文本。我希望你明白我的意思
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多