【问题标题】:Copy all text inside recyclerview android java?复制recyclerview android java中的所有文本?
【发布时间】:2022-10-19 05:49:53
【问题描述】:

我正在使用我的 android 项目,它由多个文本视图回收站视图

我已经为我的文本视图使用了android:textIsSelectable="true",但问题是它只选择了一个文本视图。

片段布局

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/text1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textIsSelectable="true"
       android:text="Title Here"/>

  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/text2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textIsSelectable="true"
       android:text=" AnotherTitle Here"/>

  <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/recyclerView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

回收站视图布局

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:text="Recycler view text here"/>

长按时如何通过选择所有文本一次复制所有文本?

【问题讨论】:

  • 为什么不添加会根据基础数据生成文本并将其复制到剪贴板的按钮?
  • @Selvin您是否有关于如何实施我认为是一个更好的主意的链接。

标签: java android xml textview


【解决方案1】:

以下是一些您可以查看的链接。 Link for usage of Clipboard Official Documentation for clipboard usage

执行

  1. 创建一个&lt;Button&gt;,上面写着“复制所有”
  2. 使用 getText() 从文本字段中获取文本
  3. 无法通过 xml 从每个回收站视图项中获取文本,因此您只能进入代码内部。因为您将向&lt;RecyclerView&gt; 的适配器发送数据,这意味着您已经拥有数据。只需使用它。
  4. 最后使用剪贴板管理器将所有这些文本复制到剪贴板
  5. 向用户显示一条消息说《复制文字》

    这是代码:如果觉得有帮助请点赞。

    XML 代码:

            <Button
                android:id="@+id/copy_all_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Copy All" />
    

    Java 代码:

    //required imports
    import android.content.ClipData;
    import android.content.ClipboardManager;
    
    //global list
    private List<String> itemsList;     // this list stores items of recycler view hence the text inside items of recycler view
    
    // other code
        Button copyAllButton = findViewById(R.id.copy_all_button);  // button for copy all
        copyAllButton.setOnClickListener(view -> copyTextToClipboard());    //handling button click
    // other code
     
    //call this method when button is clicked
    private void copyTextToClipboard() {
        TextView tv1 = findViewById(R.id.text1);
        TextView tv2 = findViewById(R.id.text2);
        String st1 = tv1.getText().toString();   // get text from first text view
        String st2 = tv2.getText().toString();      // get text from second text view
        String recyclerText = getDataFromList();    // get data of items (but we already have it)
        String finalText = st1 + st2 + recyclerText;    // finally merge all text use comma or separator as per requirement
    
        //Now copy text to clipboard using ClipboardManager
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", finalText );
        clipboard.setPrimaryClip(clip);
    
        //Finally inform the user that text has been copied
        Toast.makeText(this, "Text Copied", Toast.LENGTH_SHORT).show();
    }
    
    //This method is used to get data from all items
    private String getDataFromList() {
        StringBuilder sb = new StringBuilder();
        for (String s : itemsList) {
            sb.append(s).append(" ");
        }
        return sb.toString();
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多