一次使用十二个中的两个按钮

时间:2013-06-16 12:47:03

标签: android button

我的活动中有12个按钮。我希望以下列方式使用它们:

应该允许两个按钮一次点击,当点击这两个按钮然后执行一些操作..如果此操作成功,这两个按钮必须是“不可见的”,如果此操作不成功,则必须再次可以选择点击所有12个中的任意两个按钮..

我已经设置了此活动的布局以及所有十二个按钮。我还为所有按钮设置了onClick方法。

[ADDITION]

我的意思是只允许十二个按钮中的两个按下一次......其中两个按钮......然后将两个按钮的输出进行比较......如果它们相等则按钮是不可见的,否则它们是仍然存在,用户再次有机会点击两个按钮..

[CODE]

button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    RotateAnimation rotate = new RotateAnimation(0,90);
rotate.setFillAfter(true);
button1.startAnimation(rotate);

Random r = new Random();
int next = r.nextInt(5) + 1;
imgV1.setImageResource(images[next]);  //imageView1 is given a random image

AlphaAnimation alpha = new AlphaAnimation(0,1);
alpha.setFillAfter(true);
imgV1.startAnimation(alpha);
arg0.clearAnimation();
}});

imgV1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlphaAnimation alpha = new AlphaAnimation(1,0);
alpha.setFillAfter(true);
imgV1.startAnimation(alpha);

RotateAnimation rotate = new RotateAnimation(90,0);
rotate.setFillAfter(true);
button1.startAnimation(rotate);
arg0.clearAnimation();
}});

按钮点击给出一个随机图像..图像点击给出了按钮返回..现在我想要当点击两个按钮并且如果它们具有相同的图像时,它们都会变得不可见..但是它们都转回到按钮和用户可以再次点击两个按钮中的任何一个..

每个按钮在布局后面都有一个imageView ..

3 个答案:

答案 0 :(得分:0)

您可以使用setVisibility()方法查看(按钮)来设置其开启或关闭的可见性。

Button b = ( Button )findViewById( R.id.button1 );
b.setVisibility( b.INVISIBLE );
b.setVisibility( b.VISIBLE );

答案 1 :(得分:0)

我认为的逻辑是,

你应该全局掌握两个变量。

1用于计算按钮点击次数,第2次用于存储第一次点击操作(基于您的应用)。

我将int action=0,count=0;作为全局变量。

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(count<2){// means first click
                action=1;// button1 pressed
                count++;
            }
            else{//second click
                count=0;
                //Here perform your action and based upon it, set visibility. Previous click is available in 'action'

            }
        }
    });

对所有按钮点击重复此操作。而已。我更喜欢用自己的方法调用执行操作和设置可见性。

答案 2 :(得分:0)

K ..现在我明白了。因此,Drawable中将有6个图像。我们走了......

创建一个大小为12的整数数组来存储6个图像的id。说,int[] images={R.drawable.img1,...}; 同时Button firstClick;Drawable back;知道第一次点击的按钮。 现在,我们的onClick将为,

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if(count<2){// means first click
firstClick=(Button)v;
back=v.getBackground();
                action=images[0];// button1 pressed so index 0
v.setBackgroundResource(action);
v.setEnabled(false);
                count++;
            }
            else{//second click
                count=0;
                if(action==images[0]){
v.setBackgroundResource(action);
v.setEnabled(false);

}else{
v.setBackgroundDrawable(back); //roll back to old background

firstClick.setBackgroundDrawable(back);

}

        }
    }
});