gpt4 book ai didi

android - 如何在 TextView 中选择文本时覆盖 "Copy"、 "Share"和 "Select All"选项

转载 作者:行者123 更新时间:2023-12-03 21:22:04 25 4
gpt4 key购买 nike

我有一个 TextView用户可以在其中选择文本。默认情况下会出现以下选项:“复制”、“共享”和“全选”。
我需要用自定义选项覆盖它们。但我找不到如何做到这一点。我浏览了文档和 this nice article但也不乏。这篇文章解释了当用户按下三点按钮时如何扩展菜单,这不是我需要的。

问题:如何覆盖文本部分菜单中的默认“复制”、“共享”和“全选”选项?

这是我的 View 的样子:

<TextView
android:id="@+id/transcript"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />

在java代码中我有:
transcript.setTextIsSelectable(true);
transcript.setFocusable(true);
transcript.setFocusableInTouchMode(true);

最佳答案

您可以使用 TextView.setCustomSelectionActionModeCallback()去做这个。

文档:https://developer.android.com/reference/android/widget/TextView.html#setCustomSelectionActionModeCallback(android.view.ActionMode.Callback)

我整理了一个非常简单的应用程序来演示如何使用此功能。

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView text = (TextView) findViewById(R.id.text);

CustomActionModeCallback callback = new CustomActionModeCallback(this);
text.setCustomSelectionActionModeCallback(callback);
}
}


activity_main.xml

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:text="@string/lorem_ipsum"
android:textIsSelectable="true"/>

</FrameLayout>


CustomActionModeCallback.java

public class CustomActionModeCallback implements ActionMode.Callback {

private final Context context;

public CustomActionModeCallback(Context context) {
this.context = context;
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.clear();
mode.getMenuInflater().inflate(R.menu.menu_custom, menu);
return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == R.id.custom_one) {
Toast.makeText(context, "One!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
else if (item.getItemId() == R.id.custom_two) {
Toast.makeText(context, "Two!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
else if (item.getItemId() == R.id.custom_three) {
Toast.makeText(context, "Three!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}

return false;
}

@Override
public void onDestroyActionMode(ActionMode mode) {

}
}


menu_custom.xml

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/custom_one"
android:title="One"
app:showAsAction="never"/>

<item
android:id="@+id/custom_two"
android:title="Two"
app:showAsAction="never"/>

<item
android:id="@+id/custom_three"
android:title="Three"
app:showAsAction="never"/>

</menu>


MainActivity 中没什么可评论的或任一 xml 文件。所有的魔法都发生在 CustomActionModeCallback .

两个 onCreateActionMode()onPrepareActionMode()可用于将您的自定义菜单项添加到菜单中。如果您使用 onCreateActionMode() ,系统会在溢出菜单中添加一些额外的选项,如下所示:

enter image description here
enter image description here

如果您使用 onPrepareActionMode() ,不会添加额外的项目。

enter image description here

请注意,您必须 return true来自 onCreateActionMode()无论如何(返回 false 会导致菜单不显示),但您只需 return true来自 onPrepareActionMode()如果你真的修改了菜单。

您可以在 onActionItemClicked() 中处理用户对自定义项目的点击。 .在我的示例中,我只显示了 Toast然后关闭上下文菜单(使用 ActionMode.finish() )。在这种方法中,你应该 return true仅在您自己处理的菜单项上;返回 false 允许发生系统默认操作(例如,如果您想为用户提供选择所有文本的选项)。

最后, onDestroyActionMode()菜单关闭时调用。也许你对此有一些用处;我没有。

关于android - 如何在 TextView 中选择文本时覆盖 "Copy"、 "Share"和 "Select All"选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52764650/

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