无法在 Fragment 中 select TextView
Unable to select TextView inside a Fragment
我正在尝试 select 来自片段内的 TextView 的文本。
这是我的 TextView 的 XML。
根据 this post.
,textIsSelectable、focusable、enabled 和 longclickable 属性均设置为 true
<TextView
android:id="@+id/post_text_recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/post_divider"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autoLink="web"
android:fontFamily="serif"
android:text="@string/stall_user"
android:textColor="@android:color/black"
android:textColorHighlight="@android:color/holo_blue_light"
android:textIsSelectable="true"
android:focusable="true"
android:enabled="true"
android:longClickable="true"
android:textSize="16sp" />
我还在我的 Fragment activity 中以编程方式设置了以下内容:
text.setTextIsSelectable(true);
如果有帮助,我正在从通过包含片段的 Activity 传递的 Bundle 中获取数据,然后我使用以下方法设置 TextView:
text.setText(Html.fromHtml(data.getString("text")));
text.setTextIsSelectable(true);
我仍然无法 select 文本。我在一些 Stack Overflow post 中读到,将 width/height 设置为 "wrap_content" 允许您 select 文本(我猜是一些旧的 Android 错误)。这个技巧在另一个 activity 中对我的 recyclerview TextView 有效。在这里似乎不起作用。
感谢您的帮助!
编辑:
以下是请求的完整 Fragment 布局代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.www.ViewFragments.OriginalPostFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/hide_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text=""
android:layout_alignParentTop="true"
android:background="#eeeeee"
android:gravity="end"
android:padding="4dp"
android:layout_marginEnd="16dp"
android:textColor="@android:color/black"
android:textSize="16sp"
android:elevation="6dp"
/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/detail_wrapper"
android:background="#ffffff"
android:descendantFocusability="blocksDescendants"
android:paddingBottom="48dp"
cardview:cardCornerRadius="4dp"
cardview:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/user_image_recycle"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/post_type_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_toRightOf="@+id/user_image_recycle"
android:textStyle="italic" />
<TextView
android:id="@+id/post_title_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/post_type_recycle"
android:layout_marginStart="16dp"
android:layout_toRightOf="@+id/user_image_recycle"
android:maxLines="1"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/user_name_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/post_title_recycle"
android:layout_marginStart="16dp"
android:layout_toEndOf="@+id/user_image_recycle"
android:textColor="#0094BD"
android:textSize="16sp" />
<ImageView
android:id="@+id/post_divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/user_image_recycle"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/black" />
<TextView
android:id="@+id/post_text_recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/post_divider"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autoLink="web"
android:fontFamily="serif"
android:text="@string/stall_user"
android:textColor="@android:color/black"
android:textColorHighlight="@android:color/holo_blue_light"
android:textIsSelectable="true"
android:focusable="true"
android:enabled="true"
android:longClickable="true"
android:textSize="16sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
</FrameLayout>
原因是descendantFocusability
。在您的 CardView
布局中,您将 descendantFocusability
设置为 blocksDescendants
。
它的作用是禁用后代的可聚焦性,因此您的 TextView
永远不会获得那些聚焦事件。
有关详细信息,请参阅 here。
要使其正常工作,只需从 CardView
中删除此行
android:descendantFocusability="blocksDescendants"
我正在尝试 select 来自片段内的 TextView 的文本。 这是我的 TextView 的 XML。 根据 this post.
,textIsSelectable、focusable、enabled 和 longclickable 属性均设置为 true <TextView
android:id="@+id/post_text_recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/post_divider"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autoLink="web"
android:fontFamily="serif"
android:text="@string/stall_user"
android:textColor="@android:color/black"
android:textColorHighlight="@android:color/holo_blue_light"
android:textIsSelectable="true"
android:focusable="true"
android:enabled="true"
android:longClickable="true"
android:textSize="16sp" />
我还在我的 Fragment activity 中以编程方式设置了以下内容:
text.setTextIsSelectable(true);
如果有帮助,我正在从通过包含片段的 Activity 传递的 Bundle 中获取数据,然后我使用以下方法设置 TextView:
text.setText(Html.fromHtml(data.getString("text")));
text.setTextIsSelectable(true);
我仍然无法 select 文本。我在一些 Stack Overflow post 中读到,将 width/height 设置为 "wrap_content" 允许您 select 文本(我猜是一些旧的 Android 错误)。这个技巧在另一个 activity 中对我的 recyclerview TextView 有效。在这里似乎不起作用。
感谢您的帮助!
编辑: 以下是请求的完整 Fragment 布局代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.www.ViewFragments.OriginalPostFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/hide_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text=""
android:layout_alignParentTop="true"
android:background="#eeeeee"
android:gravity="end"
android:padding="4dp"
android:layout_marginEnd="16dp"
android:textColor="@android:color/black"
android:textSize="16sp"
android:elevation="6dp"
/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/detail_wrapper"
android:background="#ffffff"
android:descendantFocusability="blocksDescendants"
android:paddingBottom="48dp"
cardview:cardCornerRadius="4dp"
cardview:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/user_image_recycle"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/post_type_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_toRightOf="@+id/user_image_recycle"
android:textStyle="italic" />
<TextView
android:id="@+id/post_title_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/post_type_recycle"
android:layout_marginStart="16dp"
android:layout_toRightOf="@+id/user_image_recycle"
android:maxLines="1"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/user_name_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/post_title_recycle"
android:layout_marginStart="16dp"
android:layout_toEndOf="@+id/user_image_recycle"
android:textColor="#0094BD"
android:textSize="16sp" />
<ImageView
android:id="@+id/post_divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/user_image_recycle"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/black" />
<TextView
android:id="@+id/post_text_recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/post_divider"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autoLink="web"
android:fontFamily="serif"
android:text="@string/stall_user"
android:textColor="@android:color/black"
android:textColorHighlight="@android:color/holo_blue_light"
android:textIsSelectable="true"
android:focusable="true"
android:enabled="true"
android:longClickable="true"
android:textSize="16sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
</FrameLayout>
原因是descendantFocusability
。在您的 CardView
布局中,您将 descendantFocusability
设置为 blocksDescendants
。
它的作用是禁用后代的可聚焦性,因此您的 TextView
永远不会获得那些聚焦事件。
有关详细信息,请参阅 here。
要使其正常工作,只需从 CardView
android:descendantFocusability="blocksDescendants"