按钮在Android中无响应

时间:2014-12-23 04:20:11

标签: java android button

我的应用程序中有按钮,其中包含数字作为文本。当按特定顺序按下按钮时,按钮上的数字会改变,按钮应该能够再次按下,并且循环继续,数字会改变。这是第一次使用,但是当数字改变时,它们被按下似乎没有做任何事情。

public class MainActivity extends Activity {

int b1 = (int) (Math.random() * 100.0);
int b2 = (int) (Math.random() * 100.0);
int b3 = (int) (Math.random() * 100.0);
int b4 = (int) (Math.random() * 100.0);
int last = 0;
int[] nums = { b1, b2, b3, b4 };
int i = 0;
String res = "";

Button button1, button2, button3, button4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Arrays.sort(nums);

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2); 
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);

    button1.setText(Integer.toString(b1));
    button2.setText(Integer.toString(b2));
    button3.setText(Integer.toString(b3));
    button4.setText(Integer.toString(b4));

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int value = b1;
            checkOrder(value);
            TextView result = (TextView) findViewById(R.id.textView1);
            result.setText(res);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int value = b2;
            checkOrder(value);
            TextView result = (TextView) findViewById(R.id.textView1);
            result.setText(res);
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int value = b3;
            checkOrder(value);
            TextView result = (TextView) findViewById(R.id.textView1);
            result.setText(res);
        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int value = b4;
            checkOrder(value);
            TextView result = (TextView) findViewById(R.id.textView1);
            result.setText(res);

        }
    });

}

private void checkOrder(int value) {
    if (value == nums[i]) {
        if (i == 3) {
            button1.setBackgroundResource(android.R.drawable.btn_default);
            button2.setBackgroundResource(android.R.drawable.btn_default);
            button3.setBackgroundResource(android.R.drawable.btn_default);
            button4.setBackgroundResource(android.R.drawable.btn_default);
            b1 = (int) (Math.random() * 100.0);
            b2 = (int) (Math.random() * 100.0);
            b3 = (int) (Math.random() * 100.0);
            b4 = (int) (Math.random() * 100.0);
            button1.setText(Integer.toString(b1));
            button2.setText(Integer.toString(b2));
            button3.setText(Integer.toString(b3));
            button4.setText(Integer.toString(b4));
            i = 0;

        } else {
            if (value == b1) {
                button1.setBackgroundColor(Color.GREEN);
            } else if (value == b2) {
                button2.setBackgroundColor(Color.GREEN);
            } else if (value == b3) {
                button3.setBackgroundColor(Color.GREEN);
            } else if (value == b4) {
                button4.setBackgroundColor(Color.GREEN);
            }
            i++;
        }
    } else {

    }
}

我认为这与onCreate方法仅在应用程序启动时运行这一事实有关。如何让按钮的按键响应?

1 个答案:

答案 0 :(得分:3)

您可能需要重新分配

的值
int[] nums = { b1, b2, b3, b4 };
b1, b2, b3, b4方法中为checkOrder生成新值后

当您尝试第二次等同if (value == nums[i])时,条件始终为falsenums中的值是先前生成的值。

我希望有所帮助!