【问题标题】:How i can create text selection in listview item?我如何在列表视图项中创建文本选择?
【发布时间】:2015-07-30 14:25:49
【问题描述】:

我想在 TextView 中创建一个文本选择,它位于 ListView 项中。 copyText.setTextIsSelectable(true);android:textIsSelectable="true" 不起作用。自定义操作模式也不起作用。 在 LogCat 中,我得到了:

W/TextView:TextView 不支持文本选择。动作模式已取消。

我想在这张图片上创建文本选择:

【问题讨论】:

标签: java android listview selection


【解决方案1】:

我通过在 longClick 上弹出一个包含 ListView 行内容的警报对话框来编写解决此问题的方法。
alertdialog 有一个带有

的文本视图
android:textIsSelectable="true"
android:focusable="true"

用户现在可以从此对话框中选择/复制他想要的文本。

代码方面,类似这样:

lvMsg.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
   @Override
   public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int pos, long id) {

      final SMessage sms = (SMessage)lvMsg.getItemAtPosition(pos);

       final LayoutInflater layoutInflaterAndroid = LayoutInflater.from(ctx);
       View mView = layoutInflaterAndroid.inflate(R.layout.copy_text_dialog, null);
       AlertDialog.Builder alertDialogBuilderCopy = new AlertDialog.Builder(getActivity());
       alertDialogBuilderCopy.setView(mView);
       TextView copyTextView = (TextView) mView.findViewById(R.id.copyTextView);
       copyTextView.setText(sms.getContent());
       AlertDialog alertDialogAndroidCopy = alertDialogBuilderCopy.create();
       alertDialogAndroidCopy.show();

      }

}

copy_text_dialog.xml 的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Long Press to Select Text"
        android:gravity="center"
        android:layout_width="match_parent"
        android:paddingTop="15dp"
        android:paddingBottom="10dp"
        android:textSize="20sp"
        android:textColor="@color/colorPrimary"
        android:layout_height="wrap_content"
        android:id="@+id/copyTextTitle"></TextView>

    <TextView android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="No Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="20dp"
        android:gravity="center"
        android:enabled="true"
        android:textIsSelectable="true"
        android:focusable="true"
        android:longClickable="true"
        android:id="@+id/copyTextView"></TextView>

</LinearLayout>

【讨论】:

    猜你喜欢
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 2018-02-17
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多