android 禁用剪切板_Android EditText禁止复制粘贴
1,自定义EditTextpackagecom.example.ui;importandroid.annotation.SuppressLint;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.ActionMode;importandroid.view.Menu;importandro
1,自定义EditTextpackagecom.example.ui;importandroid.annotation.SuppressLint;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.ActionMode;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.widget.EditText;
@SuppressLint("NewApi")public class NoMenuEditText extendsEditText {private finalContext context;/*** This is a replacement method for the base TextView class' method of the
* same name. This method is used in hidden class android.widget.Editor to
* determine whether the PASTE/REPLACE popup appears when triggered from the
* text insertion handle. Returning false forces this window to never
* appear.
*
*@returnfalse*/
booleancanPaste() {return false;
}/*** This is a replacement method for the base TextView class' method of the
* same name. This method is used in hidden class android.widget.Editor to
* determine whether the PASTE/REPLACE popup appears when triggered from the
* text insertion handle. Returning false forces this window to never
* appear.
*
*@returnfalse*/@Overridepublic booleanisSuggestionsEnabled() {return false;
}publicNoMenuEditText(Context context) {super(context);this.context =context;
init();
}publicNoMenuEditText(Context context, AttributeSet attrs) {super(context, attrs);this.context =context;
init();
}public NoMenuEditText(Context context, AttributeSet attrs, intdefStyle) {super(context, attrs, defStyle);this.context =context;
init();
}private voidinit() {this.setCustomSelectionActionModeCallback(newActionModeCallbackInterceptor());this.setLongClickable(false);
}/*** Prevents the action bar (top horizontal bar with cut, copy, paste, etc.)
* from appearing by intercepting the callback that would cause it to be
* created, and returning false.*/
private class ActionModeCallbackInterceptor implementsActionMode.Callback {private final String TAG = NoMenuEditText.class.getSimpleName();public booleanonCreateActionMode(ActionMode mode, Menu menu) {return false;
}public booleanonPrepareActionMode(ActionMode mode, Menu menu) {return false;
}public booleanonActionItemClicked(ActionMode mode, MenuItem item) {return false;
}public voidonDestroyActionMode(ActionMode mode) {
}
}
}2,在layout.xml文件中这样使用
android:id="@+id/ddd"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="禁止复制粘贴" />
更多推荐
所有评论(0)