1

我正在尝试创建一个应用程序,其界面为两行,每行 3 个按钮。所有按钮都是黑色的,除了一个是绿色的。用户必须单击绿色按钮,当这种情况发生时,其他按钮之一(随机确定)变为绿色,而按下的按钮变为黑色。我目前停留在应用程序检查按下的按钮是绿色按钮还是错误(=黑色)按钮的部分。我一直在尝试许多不同的方式,但都失败了。有谁知道我该如何解决这个问题?

编辑:根据要求,这是代码。这是一个稍微不同的界面,其中一个按钮用于生成调用 newRandom 方法的随机 ID,而其他按钮只能检查它们是否是正确的调用 testRandom,如果是则等待另一个 newRandom 调用。

    public class SecondTestActivity extends Activity {


    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final Activity StaticSecondTestActivity = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
        setupActionBar();

        // Chiude l'applicazione dopo un certo tempo
        new CountDownTimer(21000, 1000) {
             TextView textView = (TextView) findViewById(R.id.textView);

             public void onTick(long millisUntilFinished) {
                 textView.setText("Il Test 2 finirà in  " + millisUntilFinished / 1000 +" secondi");
             }

             public void onFinish() {
                 textView.setText("Test 2 Completato!");
                 Globals g = Globals.getInstance();
                 g.setPhase(2); 
                 startMain();
                 StaticSecondTestActivity.finish();

             }
          }.start();
    }

    boolean state = false;
    int idNumber = 0;
    public void newRandom(View view) {

        int min = 1;
        int max = 6;
        Random r = new Random();
        idNumber = r.nextInt(max - min + 1) + min;
        if (!state) {
            state = false;
            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_button_uncorrect));
            View button1 = (View) findViewById(R.id.button1);
            View button2 = (View) findViewById(R.id.button2);
            View button3 = (View) findViewById(R.id.button3);
            View button4 = (View) findViewById(R.id.button4);
            View button5 = (View) findViewById(R.id.button5);
            View button6 = (View) findViewById(R.id.button6);
            View correctButton = view;
            switch (idNumber) {
            case 1:
                correctButton = button1;
                break;
            case 2:
                correctButton = button2;
                break;
            case 3:
                correctButton = button3;
                break;
            case 4:
                correctButton = button4;
                break;
            case 5:
                correctButton = button5;
                break;
            case 6:
                correctButton = button6;
                break;
            default: 
                correctButton = view;
                break;
            }
            correctButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_button_correct));
        }

    }

    public void testRandom(View view) {
        int submittedId = 0;
        View button1 = (View) findViewById(R.id.button1);
        View button2 = (View) findViewById(R.id.button2);
        View button3 = (View) findViewById(R.id.button3);
        View button4 = (View) findViewById(R.id.button4);
        View button5 = (View) findViewById(R.id.button5);
        View button6 = (View) findViewById(R.id.button6);
        View buttonBase = (View) findViewById(R.id.buttonBase);
        if (!view.equals(button1)) 
            submittedId = 1; 
        else if (!view.equals(button2)) 
            submittedId = 2; 
        else if (!view.equals(button3)) 
            submittedId = 3; 
        else if (!view.equals(button4)) 
            submittedId = 4; 
        else if (!view.equals(button5)) 
                submittedId = 5; 
        else if (!view.equals(button6)) 
            submittedId = 6; 
        else 
            submittedId = 0;
         if (state) {
             if (submittedId == idNumber) {
                 state = false;
                 view.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_button_uncorrect));
                 buttonBase.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_button_correct));

             }
         }

    }

    protected void startMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
}

    private void setupActionBar() {
        getActionBar().setDisplayHomeAsUpEnabled(true);}



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
4

0 回答 0