Android 确定一个随机视图,并将其与onClick选择的视图进行比较

Android 确定一个随机视图,并将其与onClick选择的视图进行比较,android,Android,我试图创建一个应用程序,它有两行3个按钮作为界面。除绿色按钮外,所有按钮均为黑色。用户必须单击绿色按钮,当这种情况发生时,随机确定的其他按钮之一变为绿色,而按下的按钮变为黑色。我现在被困在应用程序检查按下的按钮是绿色的还是错误的=黑色的部分。我尝试了很多不同的方法,但都失败了。有人知道我如何解决这个问题吗 编辑:根据要求,下面是代码。这是一个稍微不同的界面,其中一个按钮用于生成调用newRandom方法的随机ID,而其他人只能检查调用testRandom的人是否正确,如果正确,则等待另一个new

我试图创建一个应用程序,它有两行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);
    }

}

您可以为每个按钮设置带有颜色值的标记,并在评估验证机制时使用颜色值。@GauravArora问题是我不仅仅使用颜色,我使用的是可绘制的…请发布失败的代码,以便我们改进我注意到的一件事,所有的按钮在下一次被选中的概率是相等的。根据我的理解,您不希望再次选择同一个按钮作为新的“正确的按钮”。@ GauravArora,这是我没有考虑到的,但我仍然需要在改进之前让应用程序工作。