以编程方式添加多个带有点击方法的CardViews。

huangapple go评论63阅读模式
英文:

Add many CardViews with onclick method programmatically

问题

我对Android仍然很新,我想在每次点击按钮时向活动添加一个CardViewCardView上有文本和背景图片。我已经有可以添加这个的XML文件,但因为我想要能够添加多个,所以不能使用<include>。第一张图片是当按钮被点击一次时,第二张是当按钮被点击3次时。我已经为TextView上的onClickCardView的XML设置好了,但我不能使其能够在每个CardView中添加它们并更改TextView中的文本。我也找不到一种方法来以编程方式添加到以编程方式创建的CardViewonClick监听器。在以后,我也想能够通过点击按钮来删除CardView

以下是CardView的XML文件(在它之前是在RelativeLayout中):

<androidx.cardview.widget.CardView
    android:id="@+id/cardviewClassesBlock1"
    android:layout_width="330dp"
    android:layout_height="75dp"
    android:layout_marginTop="90dp"
    android:layout_centerHorizontal="true"
    app:cardCornerRadius="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_launcher_background">

        <TextView
            android:id="@+id/textviewClassesBlock1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="3dp"
            android:textSize="22sp"
            android:fontFamily="@font/amiko_semibold"
            android:textColor="@color/white"
            android:text="Block A"/>

        <ImageView
            android:layout_width="60dp"
            android:layout_height="6dp"
            android:layout_marginStart="10dp"
            android:layout_below="@+id/textviewClassesBlock1"
            android:background="@drawable/rounded_corner_edittext" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="5dp"
            android:textColor="@color/white"
            android:text="P - 0 | T - 0 | A - 0"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

以编程方式添加多个带有点击方法的CardViews。

以编程方式添加多个带有点击方法的CardViews。

英文:

I'm still new to the Android and I want to add a CardView to the activity each time a button is clicked. The CardView has text on it and a background image. I already have the XML file that can add this, but because I want to be able to add more than one, I can't use <include.>. The first image is when the button is clicked once and the second is when the button is clicked 3 times. I already have the onClick for the TextView that says "Click To Add Block" and the XML for the CardView, but I can't make it so that you can add them and change the text in the TextView in each and every one of them. I also can't seem to find a way to programmatically add an onClick listener to the programmatically created CardView. Later down the line, I would also like to be able to delete the CardView from a click of a button too.

以编程方式添加多个带有点击方法的CardViews。

以编程方式添加多个带有点击方法的CardViews。

Here is the CardView XML file (Before it was inside a Relative Layout)

&lt;androidx.cardview.widget.CardView
    android:id=&quot;@+id/cardviewClassesBlock1&quot;
    android:layout_width=&quot;330dp&quot;
    android:layout_height=&quot;75dp&quot;
    android:layout_marginTop=&quot;90dp&quot;
    android:layout_centerHorizontal=&quot;true&quot;
    app:cardCornerRadius=&quot;10dp&quot;&gt;

    &lt;RelativeLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;@drawable/ic_launcher_background&quot;&gt;

        &lt;TextView
            android:id=&quot;@+id/textviewClassesBlock1&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginStart=&quot;10dp&quot;
            android:layout_marginTop=&quot;3dp&quot;
            android:textSize=&quot;22sp&quot;
            android:fontFamily=&quot;@font/amiko_semibold&quot;
            android:textColor=&quot;@color/white&quot;
            android:text=&quot;Block A&quot;/&gt;

        &lt;ImageView
            android:layout_width=&quot;60dp&quot;
            android:layout_height=&quot;6dp&quot;
            android:layout_marginStart=&quot;10dp&quot;
            android:layout_below=&quot;@+id/textviewClassesBlock1&quot;
            android:background=&quot;@drawable/rounded_corner_edittext&quot; /&gt;

        &lt;TextView
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_alignParentBottom=&quot;true&quot;
            android:layout_alignParentEnd=&quot;true&quot;
            android:layout_marginEnd=&quot;5dp&quot;
            android:textColor=&quot;@color/white&quot;
            android:text=&quot;P - 0 | T - 0 | A - 0&quot;/&gt;

    &lt;/RelativeLayout&gt;

&lt;/androidx.cardview.widget.CardView&gt;

</details>


# 答案1
**得分**: 1

我已经为您创建了一个示例项目。

1. 首先,您需要为您的 `CardView` 创建一个布局。在 `res/layout` 中创建 `card_base.xml`,并添加以下内容:
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardviewClassesBlock1"
    android:layout_width="wrap_content"
    android:layout_height="75dp"
    android:layout_centerHorizontal="true"
    app:cardCornerRadius="10dp"
    android:layout_margin="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_launcher_background">

        <TextView
            android:id="@+id/textviewClassesBlock1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="3dp"
            android:text="Block A"
            android:textSize="22sp"
            />

        <ImageView
            android:layout_width="60dp"
            android:layout_height="6dp"
            android:layout_below="@+id/textviewClassesBlock1"
            android:layout_marginStart="10dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="5dp"
            android:text="P - 0 | T - 0 | A - 0"
            />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

这基本上是您的带有一些小变化的 CardView

  1. 接下来,在您的 activity_main.xml 中添加以下内容:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/butAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add New Card"
        />

    <Button
        android:id="@+id/butDoSth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Do something"
        />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/cards"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <include
            android:id="@+id/includedLayoutFirst"
            layout="@layout/card_base"
            />

    </androidx.appcompat.widget.LinearLayoutCompat>

</androidx.appcompat.widget.LinearLayoutCompat>

这是您的应用程序的初始(非常简单)外观。它有一个按钮和一个已经插入的 CardView。

  1. 现在,在您的 MainActivity.java 中粘贴以下内容:
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;

public class MainActivity extends AppCompatActivity
{
    private int starter = 66; //ASCII code for `B`
    LinearLayoutCompat cards;
    Button buttonAdd;
    Button buttonDoSth;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cards = findViewById(R.id.cards);
        buttonAdd = findViewById(R.id.butAdd);
        buttonDoSth = findViewById(R.id.butDoSth);

        buttonAdd.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                CardView newCard = new CardView(MainActivity.this);
                getLayoutInflater().inflate(R.layout.card_base, newCard);

                TextView t = newCard.findViewById(R.id.textviewClassesBlock1);

                String current = Character.toString((char) starter++);

                t.setText("Block " + current);
                newCard.setTag(current); //

                cards.addView(newCard);
            }
        });

        buttonDoSth.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                findBlockAndDoSomething("B");
            }
        });
    }

    private void findBlockAndDoSomething(String name)
    {
        Log.d("MyTAG", "CLICK");

        for (int i = 0; i < cards.getChildCount(); i++)
        {
            CardView selected = (CardView) cards.getChildAt(i);

            if (selected.getTag() != null && selected.getTag().toString().equals(name))
            {
                // do something. E.g change block name
                TextView textViewClassesBlock1 = selected.findViewById(R.id.textviewClassesBlock1);
                textViewClassesBlock1.setText("Block XXX");
                return;
            }
        }
    }
}

结果(初始代码和添加新的 CardView):

以编程方式添加多个带有点击方法的CardViews。

英文:

I have created a sample project for You.

  1. First You have to create a layout for Your CardView. In res/layout create card_base.xml. In this layout add:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.cardview.widget.CardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:id=&quot;@+id/cardviewClassesBlock1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;75dp&quot;
android:layout_centerHorizontal=&quot;true&quot;
app:cardCornerRadius=&quot;10dp&quot;
android:layout_margin=&quot;10dp&quot;
&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@drawable/ic_launcher_background&quot;
&gt;
&lt;TextView
android:id=&quot;@+id/textviewClassesBlock1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;10dp&quot;
android:layout_marginTop=&quot;3dp&quot;
android:text=&quot;Block A&quot;
android:textSize=&quot;22sp&quot;
/&gt;
&lt;ImageView
android:layout_width=&quot;60dp&quot;
android:layout_height=&quot;6dp&quot;
android:layout_below=&quot;@+id/textviewClassesBlock1&quot;
android:layout_marginStart=&quot;10dp&quot;
/&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:layout_marginEnd=&quot;5dp&quot;
android:text=&quot;P - 0 | T - 0 | A - 0&quot;
/&gt;
&lt;/RelativeLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;

This is basically Your CardView with small changes.

  1. Next, in Your activity_main.xml add this:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.appcompat.widget.LinearLayoutCompat xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/root&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.MainActivity&quot;
&gt;
&lt;Button
android:id=&quot;@+id/butAdd&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Add New Card&quot;
/&gt;
&lt;Button
android:id=&quot;@+id/butDoSth&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Do something&quot;
/&gt;
&lt;androidx.appcompat.widget.LinearLayoutCompat
android:id=&quot;@+id/cards&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
&gt;
&lt;include
android:id=&quot;@+id/includedLayoutFirst&quot;
layout=&quot;@layout/card_base&quot;
/&gt;
&lt;/androidx.appcompat.widget.LinearLayoutCompat&gt;
&lt;/androidx.appcompat.widget.LinearLayoutCompat&gt;

This is a starter (very simple) look of Your app. It has one button and one CardView which is already inserted.

  1. Now in Your MainActivity.java paste this:
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
public class MainActivity extends AppCompatActivity
{
private int starter = 66; //ASCII code for `B`
LinearLayoutCompat cards;
Button buttonAdd;
Button buttonDoSth;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = findViewById(R.id.cards);
buttonAdd = findViewById(R.id.butAdd);
buttonDoSth = findViewById(R.id.butDoSth);
buttonAdd.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CardView newCard = new CardView(MainActivity.this);
getLayoutInflater().inflate(R.layout.card_base, newCard);
TextView t = newCard.findViewById(R.id.textviewClassesBlock1);
String current = Character.toString((char) starter++);
t.setText(&quot;Block &quot; + current);
newCard.setTag(current); //
cards.addView(newCard);
}
});
buttonDoSth.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
findBlockAndDoSomething(&quot;B&quot;);
}
});
}
private void findBlockAndDoSomething(String name)
{
Log.d(&quot;MyTAG&quot;, &quot;CLICK&quot;);
for (int i = 0; i &lt; cards.getChildCount(); i++)
{
CardView selected = (CardView) cards.getChildAt(i);
if (selected.getTag() != null &amp;&amp; selected.getTag().toString().equals(name))
{
// do something. E.g change block name
TextView textViewClassesBlock1 = selected.findViewById(R.id.textviewClassesBlock1);
textViewClassesBlock1.setText(&quot;Block XXX&quot;);
return;
}
}
}
}

Result (starter code and with adding new CardView):

以编程方式添加多个带有点击方法的CardViews。

huangapple
  • 本文由 发表于 2020年8月31日 03:57:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63661601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定