如何在android studio的If语句中编写“if button clicked”?

时间:2015-09-11 16:57:19

标签: java android if-statement memory

我正在制作一张有四张牌(2x2)的记忆游戏。这四张卡上有一个名为“cards”的onClick。这个onClick包含一个If语句,如果它们不相同,则将它们翻转,如果它们相同则保留它们。

卡的正面图像对于4是相同的,但背面有不同的图像。我的问题是我想要卡片翻转,但他们已经有一个onClick。那么如何在If语句中编写“if button clicked”或者是否有其他解决方案?

编辑:

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setVisibility(View.INVISIBLE);
            pic1 = (ImageView) findViewById(R.id.imageView);
            pic2 = (ImageView) findViewById(R.id.imageView2);
            pic3 = (ImageView) findViewById(R.id.imageView3);
            pic4 = (ImageView) findViewById(R.id.imageView4);

            pic1.setImageResource(R.drawable.img1);
            pic2.setImageResource(R.drawable.img1);
            pic3.setImageResource(R.drawable.img1);
            pic4.setImageResource(R.drawable.img1);

            pic1.setVisibility(View.VISIBLE);
            pic2.setVisibility(View.VISIBLE);
            pic3.setVisibility(View.VISIBLE);
            pic4.setVisibility(View.VISIBLE);


        }
    });

}


public void cards(View v) {


    if (v.getId() == pic1.getId() ) {
        pic1.setImageResource(R.drawable.img2);
        pic1.setTag("img2");
    } else if (v.getId() == pic2.getId()) {
        pic2.setImageResource(R.drawable.img2);
        pic2.setTag("img2");
    } else if (v.getId() == pic3.getId()) {
        pic3.setImageResource(R.drawable.img3);
        pic3.setTag("img3");
    } else if (v.getId() == pic4.getId()) {
        pic4.setImageResource(R.drawable.img3);
        pic4.setTag("img3");
    }

    if (R.drawable.img2 == R.drawable.img2) {
        pic1.setImageResource(R.drawable.img2);
        pic1.getTag();
        pic2.setImageResource(R.drawable.img2);
        pic2.getTag();
    }
    if (R.drawable.img3 == R.drawable.img3) {
        pic3.setImageResource(R.drawable.img3);
        pic3.getTag();
        pic4.setImageResource(R.drawable.img3);
        pic4.getTag();

    }
    if (R.drawable.img2 != R.drawable.img2 || R.drawable.img3 != R.drawable.img3) {
        pic1.setImageResource(R.drawable.img1);
        pic2.setImageResource(R.drawable.img1);
        pic3.setImageResource(R.drawable.img1);
        pic4.setImageResource(R.drawable.img1);


    }


}

第二种方法我在尝试:@Override             public void onClick(查看v){                 button1.setVisibility(View.INVISIBLE);                 pic1 =(ImageView)findViewById(R.id.imageView);                 pic2 =(ImageView)findViewById(R.id.imageView2);                 pic3 =(ImageView)findViewById(R.id.imageView3);                 pic4 =(ImageView)findViewById(R.id.imageView4);

            pic1.setImageResource(R.drawable.img1);
            pic2.setImageResource(R.drawable.img1);
            pic3.setImageResource(R.drawable.img1);
            pic4.setImageResource(R.drawable.img1);

            pic1.setVisibility(View.VISIBLE);
            pic2.setVisibility(View.VISIBLE);
            pic3.setVisibility(View.VISIBLE);
            pic4.setVisibility(View.VISIBLE);


            if (R.drawable.img2 == R.drawable.img2) {
                pic1.setImageResource(R.drawable.img2);

                pic2.setImageResource(R.drawable.img2);

            }
            if (R.drawable.img3 == R.drawable.img3) {
                pic3.setImageResource(R.drawable.img3);

                pic4.setImageResource(R.drawable.img3);


            }
            if (R.drawable.img2 != R.drawable.img2 || R.drawable.img3 != R.drawable.img3) {
                pic1.setImageResource(R.drawable.img1);
                pic2.setImageResource(R.drawable.img1);
                pic3.setImageResource(R.drawable.img1);
                pic4.setImageResource(R.drawable.img1);


            }


        }
    });

}



public void pic1Click(View v){
    pic1.setImageResource(R.drawable.img1);
}
public void pic2Click(View v){
    pic2.setImageResource(R.drawable.img1);
}
public void pic3Click(View v){
    pic3.setImageResource(R.drawable.img2);
}
public void pic4Click(View v){
    pic4.setImageResource(R.drawable.img2);
}

3 个答案:

答案 0 :(得分:1)

所以我认为您正在寻找一种方法来确定用户点击了哪个ImageButton。您可以从传递给onClick方法的视图变量中获取此信息。

您还需要跟踪已点击的图像。您可以通过向图像添加标记来跟踪此情况。

public void myClickMethod(View v){
    if (v.getId() == pic1.getId() ) {
        pic1.setImageResource(R.drawable.img2);
        pic1.addTag("img2");
    } else if (v.getId() == pic2.getId() {
        pic2.setImageResource(R.drawable.img2);
        pic2.addTag("img2");
    } else if (v.getId() == pic3.getId() {
        pic3.setImageResource(R.drawable.img3);
        pic3.addTag("img3");
    } else if (v.getId() == pic4.getId() {
        pic4.setImageResource(R.drawable.img3);
        pic4.addTag("img3");
    } 

当然,每次更改图像时(以及第一次创建图像时)都需要设置标记。因此,要检查是否需要重置,您需要检查两个图像是否已经更改了它们开始时的默认图像。您也可以使用全局变量执行此操作,但标记可能更直观一些。

答案 1 :(得分:0)

试试这个

     protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);



        b1 = (Button)findViewById(R.id.button1);

        b1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                //write your codes here





                }

            });
}

答案 2 :(得分:0)

您可以参考Android Dev Doc

首先,您需要知道什么是事件监听器,并实施它。 完成后,您可以使用您的实现注册事件监听器。

示例代码

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mCorkyListener);
    ...
}