依次查找并获取2个单击的按钮的ID

时间:2018-08-28 11:32:16

标签: java android

我正在制作一个程序,单击相同的2张重复的图片。 如果用户在单击第一个图片后单击了错误的图片,则该值为负。如果两个图片相同,则将这些按钮设置为clickable(false)。但是我的问题是,当第二张图片与第一张图片不同时,我想将clickable(true)设置为第二张图片以及第一张图片,但是我不知道如何获取第一张点击按钮的ID。 / p>

我有16个按钮,在此,我发布了button1,其他按钮类似。 我的代码是.....

 Collections.shuffle(buttonResources);
        for(int i = 0; i < buttonResources.size(); i++)
        {
            ImageButton bt = findViewById(buttonResources.get(i));
            buttons.add(bt);
            bt.setBackgroundResource(pics.get(i/2));
            bt.setTag(pics.get(i/2));
            aaa = pics.get(i/2);
            bt.setOnClickListener(this);
            bt.setClickable(false);
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AlphaAnimation alphaAnimation0 = new AlphaAnimation(1,0);
                alphaAnimation0.setDuration(200);
                alphaAnimation0.setFillAfter(true);

                for(int i = 0; i < buttonResources.size(); i++)
                {
                    ImageButton bt = findViewById(buttonResources.get(i));
                    buttons.add(bt);
                    bt.startAnimation(alphaAnimation0);

                    bt.setClickable(true);
                }

            }
        }, 5000);




        }

    @Override
    public void onClick(final View v1) {
        switch (v1.getId()) {
            case R.id.bt1:
                AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
                alphaAnimation1.setDuration(300);
                alphaAnimation1.setFillAfter(true);
                bt1.startAnimation(alphaAnimation1);
                aaa = (int) bt1.getTag();
                bt1.setClickable(false);

                if(bbb==0){
                    bt1.setClickable(false);
                    bbb = aaa;
                }else if (bbb==aaa){
                    right++;
                    tvtext.setText("Right" + right + "wRONG" + wrong);
                    bbb=0;

                }else{
                    wrong++;
                    tvtext.setText("Right" + right + "wRONG" + wrong);
                    bbb=0;
                    AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
                    alphaAnimation.setDuration(600);
                    alphaAnimation.setFillAfter(true);
                    bt1.startAnimation(alphaAnimation);
                    bt1.setClickable(true);
                    }
                break;

1 个答案:

答案 0 :(得分:1)

添加int bbbID=0; 然后添加以下更改:

case R.id.bt1:
                AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
                alphaAnimation1.setDuration(300);
                alphaAnimation1.setFillAfter(true);
                bt1.startAnimation(alphaAnimation1);
                aaa = (int) bt1.getTag();
                bt1.setClickable(false);

                if(bbb==0){
                    bt1.setClickable(false);
                    bbb = aaa;
                    bbbID = v1.getId();   //////change
                }else if (bbb==aaa){
                    right++;
                    tvtext.setText("Right" + right + "wRONG" + wrong);
                    bbb=0;
                    bbbID = 0;   //////change

                }else{
                    wrong++;
                    tvtext.setText("Right" + right + "wRONG" + wrong);
                    bbb=0;
                    AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
                    alphaAnimation.setDuration(600);
                    alphaAnimation.setFillAfter(true);
                    bt1.startAnimation(alphaAnimation);
                    bt1.setClickable(true);
                    for(int i=0;i<buttons.size();i++){   //////change
                       if(buttons.get(i).getId() == bbbID){   //////change
                           buttons.get(i).setClickable(true);   //////change
                       }
                    }
                    }
                break;