Android Studio How to check which Imageview has cl

2019-07-15 08:59发布

问题:

ActivityOne

I have 12 Imageviews. User click on either 1 of them. If they click ImageView 1, we assign a int value to it (image_id=1; )

ActivityTwo

Use a If else statement to check which image has they clicked

Intent result_intent=getIntent();

    if (image_id==1) {
        text.setText("U have clicked image "+image_id);
    }

I need the int value in ActivityTwo as I need to calculate sth. Anyone can help? Thx

回答1:

@gosulove

setOnClickListener on your all ImageViews

Like this In your Activity 1

ImageView1.setOnClickListener(this);
ImageView2.setOnClickListener(this);
ImageView3.setOnClickListener(this);
ImageView4.setOnClickListener(this);
ImageView5.setOnClickListener(this);
ImageView6.setOnClickListener(this);
ImageView7.setOnClickListener(this);
ImageView8.setOnClickListener(this);
ImageView9.setOnClickListener(this);
ImageView10.setOnClickListener(this);
// like this for all 10 ImageViews

Now inside your onClick() method

public void onClick(View v) {
       int image_id;
       swtich v.getId() {
        case R.id.ImageView1 :
             image_id = 1;
             break;
        case R.id.ImageView2 :
             image_id = 2;
             break;
        // like this for all your imageviews
        case R.id.ImageView10 :
             image_id = 10;
             break;
        }
        Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
        intent.putExtra("IMAGE_ID", image_id);
        startActivity(intent);
      }

In your Activity2

int image_id = getIntent().getIntExtra("IMAGE_ID", 0);
text.setText("U have clicked image "+image_id);


回答2:

ImageView.setOnClickListener(new View.OnClickListener()
{
  public void onClick(View v)
  {
    //do your stuff
  }
});


回答3:

In ActivityOne, pass the value in this way:

Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putExtra("IMAGE_ID", imageId);
startActivity(intent);

In ActivityTwo, get the value in this way:

int image_id = getIntent().getIntExtra("IMAGE_ID", 0);


回答4:

Use Intent to pass the value to Activity2.

In Activity1:

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("image_id", image_id);
startActivity(intent);

In Activity2:

String image_id = getIntent.getIntExtra("image_id");

more detail



回答5:

You can setOnClickListener on each imageView. In onClick(View v) method check the id of each imageview. that is in this way:

ImageView1.setOnClickListener(this);
ImageView2.setOnClickListener(this);

Now you have to implements View.OnClickListener interface. After implementing this you have this method:

@Override
        public void onClick(View v) {
            if(v.getId()==R.id.ImageView1){
               image_id=1;
       Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
       intent.putExtra("IMAGE_ID", image_id);
       startActivity(intent);
             }else  if(v.getId()==R.id.ImageView2){
               image_id=2;
        Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
       intent.putExtra("IMAGE_ID", image_id);
       startActivity(intent);
             }
       }

Hope this will help you :)

Edit: for more about in button click visit here