Android Studio How to check which Imageview has cl

2019-07-15 08:30发布

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

5条回答
SAY GOODBYE
2楼-- · 2019-07-15 08:54

@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);
查看更多
时光不老,我们不散
3楼-- · 2019-07-15 08:56
ImageView.setOnClickListener(new View.OnClickListener()
{
  public void onClick(View v)
  {
    //do your stuff
  }
});
查看更多
女痞
4楼-- · 2019-07-15 08:58

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);
查看更多
成全新的幸福
5楼-- · 2019-07-15 09:03

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

查看更多
Melony?
6楼-- · 2019-07-15 09:11

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

查看更多
登录 后发表回答