简体   繁体   English

如何确定从Android中的按钮列表中按下了哪个按钮?

[英]How do I determine which button was pressed from a list of buttons in Android?

I have an xml file for a cardview to be used in a recyclerview in my main android class. 我在我的主要android类的recyclerview中有一个用于cardview的xml文件。 This cardview contains a button that will be used to delete the cardview from the list. 该卡片视图包含一个按钮,该按钮将用于从列表中删除卡片视图。 The problem I have encountered is that all of the buttons in the recyclerview have identical onClick values and I do not know how to differentiate between them. 我遇到的问题是,recyclerview中的所有按钮都具有相同的onClick值,并且我不知道如何区分它们。

Here is my button in the cardview xml: 这是cardview xml中的我的按钮:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete"
    android:id="@+id/btnDelete"
    android:onClick="DeleteCard"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

In my main class, I have a function DeleteCard which runs whenever I press any of the delete buttons. 在我的主班里,我有一个DeleteCard函数,只要我按任意一个删除按钮,它就会运行。 I have a TextView in my cardview as well, and I was wondering if it was possible to somehow retrieve the text that is in the TextView when I press the delete button so I know which one to delete. 我的cardview中也有一个TextView,我想知道是否有可能在按Delete键时以某种方式检索TextView中的文本,所以我知道要删除哪个文本。

This is my onBindViewHolder: 这是我的onBindViewHolder:

public void onBindViewHolder(TimeViewHolder personViewHolder, int i) {
    int min = (int)(times.get(i).time / (60 * 1000));
    int remaining = (int)(times.get(i).time % (60 * 1000));
    int sec = remaining / 1000;
    remaining = remaining % (1000);
    int mill = (remaining%1000)/10;
    DecimalFormat df = new DecimalFormat("00");
    personViewHolder.time.setText(min + ":" + df.format(sec) + "." + mill);
    personViewHolder.date.setText(times.get(i).date);
}

I want to get the text from date and use that as a parameter to remove that key in the SharedPreferences. 我想从日期获取文本,并将其用作参数以删除SharedPreferences中的键。 Here is the method that runs when a delete button is pressed: 这是按下删除按钮时运行的方法:

public void DeleteCard(View view){
    SharedPreferences.Editor editor = history.edit();
    editor.remove("Date Here");
    editor.commit();
}

Implement onclick on Button in onBindViewHolder not in xml and add a tag of position or unique identifier to this button. 在onBindViewHolder中而不是在xml中实现onclick on Button,并向该按钮添加位置标记或唯一标识符。 So when you will click then inside onclick extract tag from view object and perfrom operation which you want. 因此,当您单击然后在onclick内部单击时,将从所需的视图对象和perfrom操作中提取标签。

The view is passed to the DeleteCard method, so you can get the id of the view using view.getId() and then you can switch the id like this: 视图将传递给DeleteCard方法,因此您可以使用view.getId()获取视图的ID,然后可以像这样切换ID:

 public void DeleteCard(View view) {
    switch(view.getId()){

        case R.id.buttonDelete:
        ....
        break;

        case R.id.buttonAdd:
        ...
        break;



    }
}

Remove onClick() from the XML, and add the click listener in the onCreateViewHolder(..) method. 从XML中删除onClick() ,然后在onCreateViewHolder(..)方法中添加单击侦听器。 The position can be retrieved from the getPosition() method in the ViewHolder class which is deprecated now. 可以从现在不推荐使用的ViewHolder类的getPosition()方法中检索位置。

You can get the position using the getAdapterPosition() or getLayoutPosition() . 您可以使用getAdapterPosition()getLayoutPosition()获取位置。

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#getPosition() https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#getPosition()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM