Card View Click on Card Move To New Activity

后端 未结 6 1727
死守一世寂寞
死守一世寂寞 2021-02-07 03:39

I am new to Android programming and was working on a card layout. I was wondering, how do I make it clickable?

android:clickable=\"true\"
android:foreground=\"?         


        
6条回答
  •  独厮守ぢ
    2021-02-07 04:39

    You could implement the View.OnClickListener() interface to your class and then in your onCreate() method you could write findViewById(R.id.cardview).setOnClickListener(this). You could then Override the onClick() method and do what you want to do when the user clicks the card.

    It would look like this:

    public class MainActivity extends Activity implements View.OnClickListener()
    {
         public void onCreate(Bundle savedInstanceState)
         {
           super.onCreate(savedInstanceState);
           // load the layout
           setContentView(R.layout.filters);
           // get the id of the CardView and attach an onClickListener to it
           findViewById(R.id.cardList).setOnClickListener(this)
         }
         @Override
         private void onClick(View view)
         {
            if(view.getId == R.id.cardList)
            {
             //Do something Like starting an activity
             Intent intent = new Intent(MyActivity.this, NextActivity.class);
             startActivity(intent);
            }
         }
    }
    

提交回复
热议问题