CardView与按钮不使用RecyclerView - Android

时间:2017-03-22 03:25:49

标签: android android-studio

我正在尝试使用CardView作为菜单的容器;卡上装有按钮。是否可以在不使用RecyclerView的情况下在每个按钮上实现单击侦听器?

在创建期间,在添加到CardView之前和添加到CardView之后在按钮上应用单击侦听器不起作用。只有卡本身可以点击

2 个答案:

答案 0 :(得分:1)

在此示例中,在cardview上添加了两个按钮,其中每个视图都有单独的单击侦听器。你可以试试这个 -

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private String TAG = "MainActivity";
    Button btn_first, btn_second;
    CardView cardView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cardView = (CardView) findViewById(R.id.cardview);
        cardView.setOnClickListener(this);

        btn_first = (Button)findViewById(R.id.btn_first);
        btn_second = (Button)findViewById(R.id.btn_second);
        btn_first.setOnClickListener(this);
        btn_second.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_first:
                Toast.makeText(this, "First Button", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_second:
                Toast.makeText(this, "Second Button", Toast.LENGTH_SHORT).show();
                break;
            case R.id.cardview:
                Toast.makeText(this, "CardView", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="app.module.taman.splash.mvp.SplashActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:filterTouchesWhenObscured="true"
            android:text="Click me !!" />

    </android.support.v7.widget.CardView>


</RelativeLayout>

代码

Button button = (Button) findViewById(R.id.button);
        CardView cardView = (CardView) findViewById(R.id.card);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SplashActivity.this, "ButtonClicked", Toast.LENGTH_SHORT).show();
            }
        });

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SplashActivity.this, "card clicked", Toast.LENGTH_SHORT).show();
            }
        });