【问题标题】:How can i copy a text from the recyclerview item with a button in popup window?如何使用弹出窗口中的按钮从 recyclerview 项目中复制文本?
【发布时间】:2020-11-15 18:16:11
【问题描述】:

我有一个RecyclerViewTextView 项目,我想要实现的是制作一个类似的复制按钮,如whatsapp、电报,当您单击消息时会弹出一个窗口。 我使用PopupMenu 实现了这一点,但使用PopupWindow 我无法触发PopupWindow 中“复制”按钮的onClick 功能。

How the RecyclerView and te PopupWindow looks like

这是我的适配器 ViewHolder 的样子:

    public class MessageViewHolder extends RecyclerView.ViewHolder {
    TextView noteView;

    public MessageViewHolder(View itemView) {
        super(itemView);
        noteView = (TextView)itemView.findViewById(R.id.message_body);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.add_image_popup_window, null);
        final TextView copyMessageText = (TextView) view.findViewById(R.id.copy_message);

        noteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { // This part works fine.
                PopupWindow popupwindow = lib.popupDisplay(context);
                Drawable background = ContextCompat.getDrawable(activity, R.drawable.add_image_popup_rounded);
                popupwindow.setBackgroundDrawable(background);
                popupwindow.showAsDropDown(noteView, 0, 0);

                copyMessageText.setOnClickListener(new View.OnClickListener()
                {// This part won't get triggered when i click/touch on te "Copy" button inside the popup window.
                    @Override
                    public void onClick(View v){ 
                        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                        ClipData clip = ClipData.newPlainText("MessageText", noteView.getText().toString());
                        clipboard.setPrimaryClip(clip);
                        Toast.makeText(activity, "Text copied to clipboard", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });
    }
}

这是我的弹出窗口布局add_image_popup_window

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    >

    <TextView
        android:id="@+id/copy_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="60dp"
        android:text="@string/message_copy"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="10dp"
        android:clickable="true"
        android:textColor="@color/white"
        android:background="@drawable/copy_message_selector" />

</androidx.constraintlayout.widget.ConstraintLayout>

我该如何解决这个问题?

【问题讨论】:

    标签: java android android-studio android-recyclerview popupwindow


    【解决方案1】:

    您调用 noteView.setOnclickListener() 有点太早了。您需要将其称为您的 RecyclerView.Adapter 的 onBindView 类。

    【讨论】:

    • 我也在那里尝试过,但没有任何改变。 noteView.setOnclickListener() 仍然有效,但 copyMessageText.setOnClickListener 在那里也不起作用。
    猜你喜欢
    • 1970-01-01
    • 2012-07-13
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    相关资源
    最近更新 更多