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="禁止复制粘贴" />

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐