2 ilsswfr ilsswfr 于 2017.09.03 19:09 提问

求助!!为什么setBisibility要点击5次才有反应.

还有为什么我的提问是0啊...发了好几次

对ImageView设置点击事件,用Log看到应该是点击到了,但是点击事件里的setVisibility要点击五次才能让图片不可见,不知道为啥

全部代码
public class gamelayout extends RelativeLayout {
ImageView imageViews[]=new ImageView[7];

int cnt=0;
private int w;
private int h;

public gamelayout(Context context) {
    this(context,null);
}

public gamelayout(Context context,AttributeSet attrs) {
    this(context, attrs,0);
}
public gamelayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(1100,1100);
    init();

}

private void init(){
   for (int i=1;i<7;++i){
        imageViews[i]=new ImageView(getContext());
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(150,150);
        if (i!=1)
            params.addRule(RelativeLayout.RIGHT_OF,imageViews[i-1].getId());
        else
            params.addRule(RelativeLayout.RIGHT_OF);
        addImage(imageViews[i],i);
        addView(imageViews[i],params);
        imageViews[i].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("test",""+(++cnt));
                TES();
                v.setVisibility(INVISIBLE);
            }
        });
    }

}

private void TES() {
    Log.d("tes",""+cnt);
}

private void addImage(ImageView item,int i) {

    item.setId(i);//表示在图标中的下标
    switch(i){
        case 1:item.setImageResource(R.drawable.p1);break;
        case 2:item.setImageResource(R.drawable.p10);break;
        case 3:item.setImageResource(R.drawable.p9);break;
        case 4:item.setImageResource(R.drawable.p15);break;
        case 5:item.setImageResource(R.drawable.p16);break;
        case 6:item.setImageResource(R.drawable.p17);break;
        default:break;
    }
}

}

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.edwel.linkgametest2.MainActivity">

    <com.example.edwel.linkgametest2.gamelayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

09-03 18:28:38.332 24560-24560/com.example.edwel.linkgametest2 D/test: 1
09-03 18:28:38.332 24560-24560/com.example.edwel.linkgametest2 D/tes: 1
09-03 18:28:38.342 24560-24585/com.example.edwel.linkgametest2 D/IMGSRV: gralloc_register_buffer:1390: hnd=0x55a14130d0 ID=15392 fd=71 ref=1
09-03 18:28:38.342 24560-24585/com.example.edwel.linkgametest2 D/GraphicBuffer: register, handle(0x55a14130d0) (w:1080 h:1920 s:1088 f:0x1 u:0x000b00)
09-03 18:28:39.322 24560-24560/com.example.edwel.linkgametest2 V/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 0
09-03 18:28:39.322 24560-24560/com.example.edwel.linkgametest2 D/test: 2
09-03 18:28:39.322 24560-24560/com.example.edwel.linkgametest2 D/tes: 2
09-03 18:28:39.329 24560-24585/com.example.edwel.linkgametest2 D/IMGSRV: gralloc_register_buffer:1390: hnd=0x55a13e8870 ID=15393 fd=74 ref=1
09-03 18:28:39.329 24560-24585/com.example.edwel.linkgametest2 D/GraphicBuffer: register, handle(0x55a13e8870) (w:1080 h:1920 s:1088 f:0x1 u:0x000b00)
09-03 18:28:40.175 24560-24560/com.example.edwel.linkgametest2 V/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 0
09-03 18:28:40.175 24560-24560/com.example.edwel.linkgametest2 D/test: 3
09-03 18:28:40.175 24560-24560/com.example.edwel.linkgametest2 D/tes: 3
09-03 18:28:40.874 24560-24560/com.example.edwel.linkgametest2 V/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 0
09-03 18:28:40.874 24560-24560/com.example.edwel.linkgametest2 D/test: 4
09-03 18:28:40.874 24560-24560/com.example.edwel.linkgametest2 D/tes: 4
09-03 18:28:42.747 24560-24560/com.example.edwel.linkgametest2 V/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 0
09-03 18:28:42.747 24560-24560/com.example.edwel.linkgametest2 D/test: 5
09-03 18:28:42.747 24560-24560/com.example.edwel.linkgametest2 D/tes: 5

Csdn user default icon
上传中...
上传图片
插入图片