gpt4 book ai didi

Android:如何完全禁用 Edittext 中的复制和粘贴功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:38 29 4
gpt4 key购买 nike

我是 Android 开发领域的新手,最近我遇到了一个棘手的问题。

我试图制作一个不允许用户从中复制内容或向其粘贴内容的 Edittext。我在谷歌上搜索了很多,发现似乎有两种流行的方式:

第一种方式,在布局文件中设置:

android:longClickable="false"

第二种方式,以编程方式设置它:

myEdittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

public void onDestroyActionMode(ActionMode mode) {

}

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}

public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
return false;
}
});

但是我发现,无论我选择哪种方式,edittext区域都只能被禁止长按,从而阻止用户通过长按访问“全选、复制和粘贴”菜单。但是这两种解决方案都没有阻止用户通过简单地点击光标来访问“粘贴”功能。

所以我的问题是:我怎么能完全阻止用户在某个 Edittext 中使用复制和粘贴功能。有人帮忙吗?非常感谢

最佳答案

您可以完全隐藏“全选、复制和粘贴”菜单以及只需点击光标即可弹出的“粘贴”功能。

为此,您必须创建一个自定义的 EditText 类。这是一个例子...

// Custom EditText class
public class NoMenuEditText extends EditText
{
private final Context 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.
* @return false
*/
boolean canPaste()
{
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.
* @return false
*/
@Override
public boolean isSuggestionsEnabled()
{
return false;
}

public NoMenuEditText(Context context)
{
super(context);
this.context = context;
init();
}

public NoMenuEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context = context;
init();
}

public NoMenuEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
this.context = context;
init();
}

private void init()
{
this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
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 implements ActionMode.Callback
{
private final String TAG = NoMenuEditText.class.getSimpleName();

public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false; }
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
public void onDestroyActionMode(ActionMode mode) {}
}
}

在您的布局中使用此 EditText。现在,它不会显示任何复制/粘贴菜单。它只会显示蓝色 handle ,但当您单击它时,您不会弹出任何粘贴选项。

希望这有助于...

关于Android:如何完全禁用 Edittext 中的复制和粘贴功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175041/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com