Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何知道在ImageView上设置了哪个图像?_Android_Android Imageview - Fatal编程技术网

Android 如何知道在ImageView上设置了哪个图像?

Android 如何知道在ImageView上设置了哪个图像?,android,android-imageview,Android,Android Imageview,我有一个ImageView,它可以有两个图像中的任何一个(比如图像1和图像2,存储在drawable中)。如果ImageView包含image1,我希望禁用该ImageView,如果显示图像2,则单击图像应更改为image1并在ImageView上禁用单击。 我找不到知道ImageView上当前显示的图像的方法 这是我的密码 ImageView select = (ImageView) view.findViewById(R.id.select); select.setOnClickListe

我有一个ImageView,它可以有两个图像中的任何一个(比如图像1和图像2,存储在drawable中)。如果ImageView包含image1,我希望禁用该ImageView,如果显示图像2,则单击图像应更改为image1并在ImageView上禁用单击。
我找不到知道ImageView上当前显示的图像的方法

这是我的密码

ImageView select = (ImageView) view.findViewById(R.id.select); 
select.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
// TODO Auto-generated method stub 
} 
});

我认为最干净的方法是将此状态与UI分开跟踪,并让UI反映您感兴趣的状态。也就是说,如果它是一个启用/禁用的表示,请跟踪是否使用适当的setter方法在布尔值中启用它,并让该方法更新UI。这也可能是一个很好的应用。

也许你可以考虑创建自己的扩展视图。 选中状态为图像,非选中状态为另一个。

这是用于按钮:

final Button whichLane = (Button) reportDialog.findViewById(R.id.which_side_icon);
        // if button is clicked, close the custom dialog
        whichLane.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (whichLane.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.other_side).getConstantState()) ) {
                    Toast.makeText(HomeActivity.this, "text", 10).show();
                }
            }
        });

将id设置为ImageView,如下所示select.setId(imageArray[i]) 然后做你想做的事

public static int[] imageArray = {
         R.drawable.image1,
         R.drawable.image2

         };
Bitmap bmp= BitmapFactory.decodeResource(getResources(),imageArray[i]);
    select.setImageBitmap(bmp);
    select.setId(imageArray[i]);
    select.setOnClickListener(new OnClickListener() {
     @Override
    public void onClick(View v) {
         if(v.getId()==R.drawable.image1){
                   //here you set image2 to select 
             } 
         else{
                  //here you set image1 to select
         }
    } } );

您可以使用
整数
并在更改图像时更改其值。
例如,使用

public Integer num; 
在视图上设置image1时,将num的值更改为1。 在
onClickListener
方法中检查num是否为1。 i、 e


更改为图像2,否则更改为图像1

您可以在imageview中使用
标记
,并相应地检查图像。然后,当您更改图像时,再次为图像视图设置
标记。例如:-

这里我展示了8幅图片的示例。我首先从布局文件中设置了名为
p1
的图像,并将图像视图标记设置为
1
。然后,当用户点击图像时,我检查
imageview
标记名。并相应地更改图像

   imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String tagId = imageView.getTag().toString();
            switch (tagId) {
                case "1":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p2));
                    imageView.setTag("2");
                    break;
                case "2":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p3));
                    imageView.setTag("3");
                    break;
                case "3":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p4));
                    imageView.setTag("4");
                    break;
                case "4":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p5));
                    imageView.setTag("5");
                    break;
                case "5":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p6));
                    imageView.setTag("6");
                    break;
                case "6":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p7));
                    imageView.setTag("7");
                    break;
                case "7":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p8));
                    imageView.setTag("8");
                    break;

            }

        }
    });

我也面临同样的问题,我可以告诉你我做了什么。我发现这是最简单的

public class MainActivity extends AppCompatActivity {
ImageView iv;
Button btn;
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    iv = findViewById(R.id.imageView);
    btn = findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(i==0)
            {
                iv.setImageResource(R.drawable.baloon); //baloon -> first image
                i=1;
            }
            else
            {
                i = 0;
                iv.setImageResource(R.drawable.sky); //sky -> second image
            }
        }
    });
}
}


注意:-在xml中,我使用sky作为图像视图的初始图像,这一点很重要,否则我们的图像在第一次单击时不会更改。

您能分享您到目前为止所做的工作吗?ImageView select=(ImageView)view.findViewById(R.id.select);选择.setOnClickListener(新的OnClickListener(){public void onClick(视图v){//TODO自动生成的方法存根}});在监听器中,如何知道imageview中当前显示的是哪个图像(给定id为select)。可能是一个更有用的示例我知道它太长,无法回复,但它可能会帮助其他人。
public class MainActivity extends AppCompatActivity {
ImageView iv;
Button btn;
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    iv = findViewById(R.id.imageView);
    btn = findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(i==0)
            {
                iv.setImageResource(R.drawable.baloon); //baloon -> first image
                i=1;
            }
            else
            {
                i = 0;
                iv.setImageResource(R.drawable.sky); //sky -> second image
            }
        }
    });
}