我有一组四个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。
我该如何实现期望的行为呢?