用吐司的Imageview

时间:2017-12-18 17:16:01

标签: android image imageview toast

我有一个项目。但是当我点击图片时,它会将我重定向到img_3。我希望当我点击时:

 img_1 and toast 1
 img_2 and toast 2
 img_3 and toast 3

并保持相同的图像。

请告诉我,我该如何编写代码?

这是代码:

public class MainActivity extends AppCompatActivity {

    ImageView view;
    Random r;
    Button button;
    Integer[] images= {
            R.drawable.img_1,
            R.drawable.img_2,
            R.drawable.img_3,
    };

    int pickedImage = 0,lastPicked = 0;

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

        button = (Button) findViewById(R.id.button);
        r = new Random();
        view = (ImageView)findViewById(idImageView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                do {
                    pickedImage = r.nextInt(images.length);
                } while (pickedImage == lastPicked);

                lastPicked = pickedImage;

                view.setImageResource(images[pickedImage]);


            }

        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Toast toast1 = Toast.makeText(getBaseContext(),"ptak 1", Toast.LENGTH_SHORT);
                view.setImageResource(R.drawable.img_1);

                toast1.show();

            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Toast toast2 = Toast.makeText(getBaseContext(),"ptak 2", Toast.LENGTH_SHORT);
                view.setImageResource(R.drawable.img_2);
                toast2.show();

            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Toast toast3 = Toast.makeText(getBaseContext(),"ptak 3", Toast.LENGTH_SHORT);
                view.setImageResource(R.drawable.img_3);
                toast3.show();

            }
        });

    }

}

3 个答案:

答案 0 :(得分:1)

因为您在单个视图上多次设置OnClick Listener,所以它将覆盖最后一个的所有其他侦听器。因此,toast-3的原因显示了eveytime。

相反,你打电话做这样的事情:

不需要使用do while循环使其变得复杂:

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            pickedImage = r.nextInt(images.length);

            view.setImageResource(images[pickedImage]);


        }

    });

在吐司之后不需要设置view.setImageResource。

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (pickedImage) {
                    case 0:
                        Toast toast1 = Toast.makeText(getBaseContext(), "ptak 1", Toast.LENGTH_SHORT);

                        toast1.show();

                        break;
                    case 1:
                        Toast toast2 = Toast.makeText(getBaseContext(), "ptak 2", Toast.LENGTH_SHORT);

                        toast2.show();
                        break;
                    case 2:
                        Toast toast3 = Toast.makeText(getBaseContext(), "ptak 3", Toast.LENGTH_SHORT);

                        toast3.show();
                        break;

                }

            }
        });

此代码完美运行(已测试)

点击按钮将获取3中的任意随机图像。

点击图片将为该图片提供祝词。 (像Image-1 to Toast-1)

答案 1 :(得分:0)

像这样使用自定义Toast

    Toast toast = new Toast(this);
    toast.setGravity(Gravity.CENTER,100,100);
    toast.setView(view);

答案 2 :(得分:0)

不要三次添加点击监听器。它总是需要最后一个听众。试试如下,

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

            button = (Button) findViewById(R.id.button);
            r = new Random();
            view = (ImageView)findViewById(idImageView);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    do {
                        pickedImage = r.nextInt(images.length);
                    } while (pickedImage == lastPicked);

                    lastPicked = pickedImage;

                    view.setImageResource(images[pickedImage]);


                }

            });
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(lastPick == 0){
                    Toast toast1 = Toast.makeText(getBaseContext(),"ptak 1", Toast.LENGTH_SHORT);
                    view.setImageResource(R.drawable.img_1);

                    toast1.show();
                 }else if(lastPick ==1){
                    Toast toast2 = Toast.makeText(getBaseContext(),"ptak 2", Toast.LENGTH_SHORT);
                    view.setImageResource(R.drawable.img_2);
                    toast2.show();
                 }else if(lastPick ==2){
                    Toast toast3 = Toast.makeText(getBaseContext(),"ptak 3", Toast.LENGTH_SHORT);
                    view.setImageResource(R.drawable.img_3);
                    toast3.show();
                  }

                }
            });



        }

    }