隐藏时,Android ImageView会引发OnClick事件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了隐藏时,Android ImageView会引发OnClick事件相关的知识,希望对你有一定的参考价值。

我有几个关于android视图的问题。我是新手,所以如果我的问题看起来很荒谬,请给我正确的提示:)所以我正在尝试制作记忆游戏。这是我的mainLayout代码。

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainPanel"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android.background="Color.TRANSPARENT"
    android:gravity="top"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/top_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Blaaaaaa1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Blaaaaaa2" />
    </LinearLayout>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:ems="3"
        android:inputType="textPassword"
        android:text="dsdsdsd"
        android:visibility="invisible" >

        <requestFocus />
    </EditText>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/top_panel"
        android:paddingBottom="50dp"
        android:paddingTop="5dp"
        android:weightSum="5" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:weightSum="5" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_a1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_a2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_a3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_a4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_a5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:weightSum="5" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_b1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_b2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_b3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_b4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_b5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:weightSum="5" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_c1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_c2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_c3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_c4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_c5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:weightSum="5" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_d1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_d2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_d3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_d4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_d5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:weightSum="5" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_e1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_e2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_e3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_e4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/pic_e5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>
    </TableLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/pic9214" />

</RelativeLayout>

这是mainActivity的代码。

public class MainActivity extends Activity 
    private OnClickListener paSharedOnClickListener = new OnClickListener();
    private ImageView paPreviewView ;
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        getWindow().setBackgroundDrawableResource(R.drawable.old_paper_r);



        ImageView img; 

        img = (ImageView) MainActivity.this.findViewById(R.id.imageView1) ;
        img.setVisibility(-1);
        this.paPreviewView = img;

        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_a1);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_a2);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_a3);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_a4);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_a5);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_b1);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_b2);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_b3);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_b4);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_b5);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_c1);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_c2);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_c3);
        img.setVisibility(-1);
        img = (ImageView) findViewById(R.id.pic_c4);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_c5);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_d1);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_d2);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_d3);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_d4);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_d5);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_e1);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_e2);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_e3);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_e4);
        img.setOnClickListener(paSharedOnClickListener);
        img = (ImageView) findViewById(R.id.pic_e5);
        img.setOnClickListener(paSharedOnClickListener);

    


    private class OnClickListener implements View.OnClickListener
    
        private boolean paIsPreviewShowed = false;

        public void onClick(View arg0) 
        

            ImageView img  = (ImageView)arg0;

            Log.v("Click", String.format("ID %s %s", img.getId() , (ImageView) MainActivity.this.findViewById(R.id.imageView1) == img ) );


            if (!this.paIsPreviewShowed)
            

                this.paIsPreviewShowed = true;

                Animation fadeIn = new AlphaAnimation( 0, 1);
                fadeIn.setDuration(150);


                AnimationSet animationSet = new AnimationSet(true);

                ScaleAnimation scaleAnim = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
                scaleAnim.setInterpolator(new LinearInterpolator());
                scaleAnim.setRepeatCount(Animation.ZORDER_NORMAL);
                scaleAnim.setDuration(250);

                TranslateAnimation move = new TranslateAnimation(0, 0, 500,0);
                move.setDuration(250);


                animationSet.addAnimation(fadeIn);
                animationSet.addAnimation(scaleAnim);
                animationSet.setFillAfter(true);





                Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.pic9012);
                if  (paPreviewView.getParent()==null )
                
                    RelativeLayout top_panel  = (RelativeLayout) MainActivity.this.findViewById(R.id.mainPanel);
                    top_panel.addView(paPreviewView);
                
                paPreviewView.setImageBitmap(bMap);
                paPreviewView.startAnimation(animationSet);


            else
            

                Animation fadeOut = new AlphaAnimation( 1, 0);
                fadeOut.setFillAfter(true);
                fadeOut.setDuration(250);
                paPreviewView.startAnimation(fadeOut);
                this.paIsPreviewShowed = false;

                ((RelativeLayout) paPreviewView.getParent()).removeView(paPreviewView);
            

                


    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    

好吧,在创建Activiti之后的第一步中,我将OnClickListener设置为每张卡片(pic_a1,pic_a2 ... pic_e5)和ImageView paPreviewView的可见性为隐形(我想要显示所选卡片的图像)。然后,当我点击pic_a1是paPreviewView显示。还行吧。我的问题是。

1)在我的这个例子的早期版本中,我通过调用隐藏了paPreviewView ImageView

paPreviewView.setVisibility(View.INVISIBLE);

要么

paPreviewView.setVisibility(View.GONE);

但它没有正常工作。因为方法OnClickListener.onClick仅从隐藏的paPreviewView ImageView调用。所以我试图从paPreviewView中删除OnClickListener,但它没有帮助。只有工作解决方案被删除(用于隐藏)并从/向mainPanel添加(用于显示)paPreviewView。

我的问题是为什么?为什么paPreviewView在隐藏paPreviewView时会引发onClick事件?

2)我的第二个问题是。比我为这个例子做的更好的解决方案(我的意思是纸牌游戏)?

非常感谢任何想法:)

答案

在这种情况下使用的正确函数是:

paPreviewView.setClickable(false);
另一答案

ImageView.setVisibility()只改变视图的“可见性”,它仍然有监听器注册。

您可以使用:

paPreviewView.setEnabled(false);

不接收点击事件。

以上是关于隐藏时,Android ImageView会引发OnClick事件的主要内容,如果未能解决你的问题,请参考以下文章

listitem中的Android listview imageview在滚动时隐藏

在按钮单击时显示/隐藏 ImageView

Android 眼睛 显示隐藏密码(ImageView)

imageView.setImageBitmap图像空白(Android 4.4)

Android:将可见设置为在运行时消失会留下一个空白空间

在 imageview 中使用时从相机或画廊拍摄的图片其方向发生变化,有时在 Android 中会垂直拉伸