Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.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 XML ImageView更改图像_Android_Image_Layout_Imageview - Fatal编程技术网

Android XML ImageView更改图像

Android XML ImageView更改图像,android,image,layout,imageview,Android,Image,Layout,Imageview,我正在尝试一些函数,基本上是在学习一些东西,我不知道为什么这不起作用。我的意思是,当我第一次触摸时,图像变为R.drawable.buttondown,但当我移动手指或松开它时,其他情况下不起作用,图像不再改变。你能告诉我为什么吗 icon = (ImageView)findViewById(R.id.imageView1); icon.setOnTouchListener(new OnTouchListener(){ @Override pu

我正在尝试一些函数,基本上是在学习一些东西,我不知道为什么这不起作用。我的意思是,当我第一次触摸时,图像变为R.drawable.buttondown,但当我移动手指或松开它时,其他情况下不起作用,图像不再改变。你能告诉我为什么吗

    icon = (ImageView)findViewById(R.id.imageView1);

    icon.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                icon.setImageResource(R.drawable.buttondown);
                break;
            case MotionEvent.ACTION_UP:
                icon.setImageResource(R.drawable.ic_launcher);
                break;
            case MotionEvent.ACTION_MOVE:
                icon.setImageResource(R.drawable.abc_ab_bottom_transparent_dark_holo);
                break;
            }
            return false;
    }

您需要返回true以通知android您已使用该事件

public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            icon.setImageResource(R.drawable.buttondown);
            break;
        case MotionEvent.ACTION_UP:
            icon.setImageResource(R.drawable.ic_launcher);
            break;
        case MotionEvent.ACTION_MOVE:
            icon.setImageResource(R.drawable.abc_ab_bottom_transparent_dark_holo);
            break;
        }
        return true;
}
从文档:

如果侦听器已使用事件,则为True,否则为false


如果您返回true,那么您是说您刚刚消费了此事件,并且对其他事件感兴趣。如果返回false,这将保留操作,因为事件未被消耗。

您能解释原因吗?此外,我还为该ImageView添加了一个空的OnClickListener,它也可以工作。如果你返回false,你是在告诉android你没有使用这个监听器,你将在3分钟内选择这个作为最佳。这样就不会调用其他侦听器。如果您返回true,那么android将停止处理该ACTION\u DOWN事件。因此,它将处理其他信息,以便在此处进行更好的信息检查: