通过使用以下EditText代码,您可以获取Cut/Copy/Paste
事件,并且必须像下面一样创建editText。在事件触发后,您可以隐藏软键盘...
<com.example.textlink.EditTextMonitor
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:hint="EditText" />
在您的包中创建一个Java文件...
public class EditTextMonitor extends EditText {
private final Context mcontext;
public EditTextMonitor(Context context) {
super(context);
this.mcontext = context;
}
public EditTextMonitor(Context context, AttributeSet attrs) {
super(context, attrs);
this.mcontext = context;
}
public EditTextMonitor(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mcontext = context;
}
@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;
}
public void onTextCut() {
InputMethodManager imm = (InputMethodManager) getContext()
.getApplicationContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Toast.makeText(mcontext, "Event of Cut!", Toast.LENGTH_SHORT).show();
}
public void onTextCopy() {
InputMethodManager imm = (InputMethodManager) getContext()
.getApplicationContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Toast.makeText(mcontext, "Event of Copy!", Toast.LENGTH_SHORT).show();
}
public void onTextPaste() {
InputMethodManager imm = (InputMethodManager) getContext()
.getApplicationContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Toast.makeText(mcontext, "Event of Paste!", Toast.LENGTH_SHORT).show();
}}
edittext.requestFocus();
。 - Harshad Pansuriya