我打算用什么代码?

问题描述:

如果我将按钮更改为ImageButtons,我可以使用哪些代码,我可以使用图片而不是字母。这是我的代码仅使用按钮。



What codes am i going to use if i will change the buttons here into ImageButtons where i can play with pictures and not letters. Here's my code using Buttons only.

btn1 = (Button) findViewById(R.id.btn1);
       btn2 = (Button) findViewById(R.id.btn2);
       btn3 = (Button) findViewById(R.id.btn3);
       btn4 = (Button) findViewById(R.id.btn4);
       btn5 = (Button) findViewById(R.id.btn5);
       btn6 = (Button) findViewById(R.id.btn6);

       btn1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               CheckButton(btn1, btn2);
               CheckButton(btn1, btn4);

           }
           });
           btn2.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               CheckButton(btn2, btn1);
               CheckButton(btn2, btn3);
               CheckButton(btn2, btn5);
           }
           });
           .........and so on.
   }

        private void CheckButton(final Button btn1, final Button btn2) {
                   if (btn2.getText().equals(""))
                   {
                       btn2.setText(btn1.getText().toString());
                       btn1.setText("");
                   }


            }







这是我的图像按钮代码,它有.setBackground(null)的问题。








Here's my codes for Image Buttons which has problems with ".setBackground(null)".


ibtn1 = (ImageButton) findViewById(R.id.ibtn1);
       ibtn2 = (ImageButton) findViewById(R.id.ibtn2);

       ibtn1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               CheckiButton(ibtn1, ibtn2);


           }
           });
       ibtn2.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               CheckiButton(ibtn2, ibtn1);


           }
           });
private void CheckiButton(final ImageButton ibtn1, final ImageButton ibtn2) {
                   if (ibtn2.getBackground().equals(""))
                   {
                       ibtn2.setBackground(ibtn1.getBackground());
                       ibtn1.setBackgroundResource("");
                   }


            }

你还必须改变布局中的类型(在res \layout中找到)。它是XML,实际控件定义为Button,需要更改为ImageButton。



祝你好运!
You must also change the type in the layout (found in res\layout). It is XML and there the actual control is defined as Button and needs to be changed to ImageButton.

Good luck!