【问题标题】:Android TextView empty area long-touch context menu for TextView, not text selection menu用于TextView的Android TextView空白区域长按上下文菜单,而不是文本选择菜单
【发布时间】:2021-10-12 12:38:53
【问题描述】:

似乎在启用了文本选择的TextView 上,如果我长按空白区域,则会选择最近的单词并显示文本选择上下文菜单。是否可以更改此设置,并为 TextView 本身显示不同的上下文菜单,而不选择最近的单词?

我的意思是:长按某个单词会显示所选单词的上下文菜单(例如“复制/剪切/全选”),长按空白区域会显示 @ 的上下文菜单987654325@(类似于“重新加载页面”)。


PS:人们似乎并不理解“空白空间”。用黄线标记的区域就是我的意思。

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                    android:id="@+id/textview1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FF0000"
                    android:text="@string/large_text"
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    android:textColor="@color/black"
                    android:textIsSelectable="true" />
    </ScrollView>
</RelativeLayout>

【问题讨论】:

  • 你所说的“空白区域”到底是什么意思,你能提供一些解释或图片吗?它是单词之间、行之间或文本周围的区域?
  • 我想我明白你在找什么,你可以用TextView + View 来做,使TextView 宽度wrap_content,所以如果用户长按空白区域(在本例中为查看)显示您的菜单
  • 我只是指我们看到的视觉空白区域。从技术上讲,我可以在单词之间或两个段落之间的空行,但是我添加的屏幕截图顶部的“材料隐喻”和“是”之间的空格在视觉上是不同的。

标签: android textview android-widget


【解决方案1】:

您可以使用TextView + View 来实现,这是一个示例:

XML:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="100dp">

    <View
        android:id="@+id/emptyArea"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Some text"
        android:textColor="@color/black"
        android:textIsSelectable="true"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

Java:

 findViewById(R.id.emptyArea).setOnLongClickListener(v -> {
            Toast.makeText(getApplicationContext(), "On emptyArea clicked", Toast.LENGTH_SHORT).show();
            return true;
        });

或者您可以使用setCustomSelectionActionModeCallback()

TextView textView = findViewById(R.id.text);
        textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {

                return false;
            }

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

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

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        });

请注意,由于某种原因,setCustomSelectionActionModeCallback 是带有 MIUI(小米设备)的not working

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多