用于多个imageView的按钮OnClickListener

时间:2015-11-03 08:50:58

标签: android android-imageview onclicklistener

有没有办法缩短按钮点击监听器的空白,因为我有多个imageView。由于我刚刚开始时缺乏经验,如果您能告诉我您修改后的代码版本,我将不胜感激。希望得到你的理解。谢谢!

爪哇

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

ImageView imgFront;
ImageView imgBack;
ImageView imageView3;
ImageView imageView4;

boolean isBackVisible = false; // Boolean variable to check if the back image is visible currently

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

    imgFront = (ImageView)findViewById(R.id.imgFront);
    imgBack = (ImageView)findViewById(R.id.imgBack);
    imageView3 = (ImageView)findViewById(R.id.imageView3);
    imageView4 = (ImageView)findViewById(R.id.imageView4);

    final AnimatorSet setRightOut = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(),
            R.animator.flight_right_out);

    final AnimatorSet setLeftIn = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(),
            R.animator.flight_left_in);

    imgFront.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!isBackVisible) {
                setRightOut.setTarget(imgFront);
                setLeftIn.setTarget(imgBack);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imgBack);
                setLeftIn.setTarget(imgFront);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;

            }

        }
    });
    imageView3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!isBackVisible) {
               setRightOut.setTarget(imageView3);
               setLeftIn.setTarget(imageView4);
               setRightOut.start();
               setLeftIn.start();
               isBackVisible = true;
            } else {
               setRightOut.setTarget(imageView4);
               setLeftIn.setTarget(imageView3);
               setRightOut.start();
               setLeftIn.start();
               isBackVisible = false;
             }
          }
       });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

3 个答案:

答案 0 :(得分:2)

一种可能的选择是:

img1.setOnClickListener(this);
img2.setOnClickListener(this);
img3.setOnClickListener(this);

并使您的Activity实现View.OnClickListener。然后在重写OnClick方法中你可以做一些事情:

switch(view.getId()) {
  case R.id.img1:
    //Do something
    break;
  case R.id.img2:
    //Do something
    break;
  case R.id.img3:
   //Do something
    break;
}

在您的代码中:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  ImageView imgFront;
  ImageView imgBack;
  ImageView imageView3;
  ImageView imageView4;

  boolean isBackVisible = false; // Boolean variable to check if the back image is visible currently

  AnimatorSet setRightOut;
  AnimatorSet setLeftIn;

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

    setRightOut = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.flight_right_out);
    setLeftIn = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.flight_left_in);

    imgFront = (ImageView) findViewById(R.id.imgFront);
    imgBack = (ImageView) findViewById(R.id.imgBack);
    imageView3 = (ImageView) findViewById(R.id.imageView3);
    imageView4 = (ImageView) findViewById(R.id.imageView4);

    imgFront.setOnClickListener(this);
    imgBack.setOnClickListener(this);
    imageView3.setOnClickListener(this);
    imageView4.setOnClickListener(this);
  }

  @Override
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.imgFront:
        if (!isBackVisible) {
          setRightOut.setTarget(imgFront);
          setLeftIn.setTarget(imgBack);
          setRightOut.start();
          setLeftIn.start();
          isBackVisible = true;
        } else {
          setRightOut.setTarget(imgBack);
          setLeftIn.setTarget(imgFront);
          setRightOut.start();
          setLeftIn.start();
          isBackVisible = false;
        }
        break;
      case R.id.imageView3:
        if (!isBackVisible) {
          setRightOut.setTarget(imageView3);
          setLeftIn.setTarget(imageView4);
          setRightOut.start();
          setLeftIn.start();
          isBackVisible = true;
        } else {
          setRightOut.setTarget(imageView4);
          setLeftIn.setTarget(imageView3);
          setRightOut.start();
          setLeftIn.start();
          isBackVisible = false;
        }
        break;
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
}

答案 1 :(得分:1)

您可以实现View.OnClickListener,也可以为每个Button设置onclick,以便在按下ImageView时调用该方法我将添加两个示例

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


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

    imgFront = (ImageView)findViewById(R.id.imgFront);
    imgBack = (ImageView)findViewById(R.id.imgBack);
    imageView3 = (ImageView)findViewById(R.id.imageView3);
    imageView4 = (ImageView)findViewById(R.id.imageView4);


    imgFront.setOnClickListener(this);
    imgBack.setOnClickListener(this);
    imageView3.setOnClickListener(this);
    imageView4.setOnClickListener(this);

}

@Override
    public void onClick(View v) {
        switch(view.getId()) {
          case R.id.imgFront:
            //Do something
            break;
          case R.id.imgBack:
            //Do something
            break;
          case R.id.imageView3:
           //Do something
            break;
          case R.id.imageView4:
           //Do something
            break;
        }
    }

}

或者您可以在每个ImageView中的xml中添加这些行

android:clickable="true"
android:onClick="methodName"

在主要内容中,您将拥有类似

的方法
public void method(View view) {
        switch(view.getId()) {
          case R.id.imgFront:
            //Do something
            break;
          case R.id.imgBack:
            //Do something
            break;
          case R.id.imageView3:
           //Do something
            break;
          case R.id.imageView4:
           //Do something
            break;
        }
    }

}

答案 2 :(得分:0)

我相信,因为你的Click代码在所有条件下基本相同,你应该创建实现onClickListener的内部类并且有2个字段

class MyClickListener implements View.OnClickListener{

    private ImageView im1,im2;
    public MyClickListener(ImageView imageView,ImageView imageView2){
        this.im1=imageView;
        this.im2=imageView2;
    }
    @Override
    public void onClick(View v) {
            if (!isBackVisible) {
                setRightOut.setTarget(im1);
                setLeftIn.setTarget(im2);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
                } else {
                    setRightOut.setTarget(im2);
                    setLeftIn.setTarget(im1);
                    setRightOut.start();
                    setLeftIn.start();
                    isBackVisible = false;
    }

就这样使用它:

imgFront.setOnClickListener(new MyClickListener(imgFront,imgBack));