0

我的禁用按钮有问题。

我的代码

public class try1 extends Activity {
private String[] myString1;
private static final Random rgenerator = new Random();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources();

    myString1 = res.getStringArray(R.array.primary);


    final Button btn1;
    btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final String q = myString1[rgenerator.nextInt(myString1.length)];
            ((Button) v).setText(q);

            if( findViewById(R.id.button5).getVisibility() == View.VISIBLE)

            {
                Toast.makeText(getBaseContext(), "Press Next to Go", Toast.LENGTH_SHORT).show();             

                    }
        }
    });
    final Button btn2;
    btn2 = (Button) findViewById(R.id.button2);
    btn2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final String q = myString1[rgenerator.nextInt(myString1.length)];
            ((Button) v).setText(q);

            if( findViewById(R.id.button5).getVisibility() == View.VISIBLE)

            {
                Toast.makeText(getBaseContext(), "Press Next to Go", Toast.LENGTH_SHORT).show();             

                    }
        }
    });
    final Button btn3;
    btn3 = (Button) findViewById(R.id.button3);
    btn3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final String q = myString1[rgenerator.nextInt(myString1.length)];
            ((Button) v).setText(q);

            if( findViewById(R.id.button5).getVisibility() == View.VISIBLE)

            {
                Toast.makeText(getBaseContext(), "Press Next to Go", Toast.LENGTH_SHORT).show();             

                    }
        }
    });
    final Button check;
    check = (Button) findViewById(R.id.button4);
    check.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final String q = myString1[rgenerator.nextInt(myString1.length)];
            ((Button) v).setText(q);

            if (btn1.getText().equals(" ")||(btn2.getText().equals(" ")||(btn3.getText().equals(" "))))
            {
                    Toast.makeText(getBaseContext(), "choose answer", Toast.LENGTH_SHORT).show();            

                        }
            else if (btn1.getText().toString().equals(myString1[1])||(btn1.getText().toString().equals(myString1[2])||
                    (btn2.getText().toString().equals(myString1[0])||(btn2.getText().toString().equals(myString1[2])||
                            (btn3.getText().toString().equals(myString1[0])||(btn3.getText().toString().equals(myString1[1])
                                    ))))))
                                    {
                                Toast.makeText(getBaseContext(), "wrong answer", Toast.LENGTH_SHORT).show();             

                                    }

                            else if (btn1.getText() == btn2.getText()|| (btn1.getText() == btn3.getText()||
                                    (btn2.getText() == btn3.getText())))
                                    {
                    Toast.makeText(getBaseContext(), "something same", Toast.LENGTH_SHORT).show();           

                        }
                            else if(btn1.getText().toString().equals(myString1[0]) &&
                                    (btn2.getText().toString().equals(myString1[1]) &&
                                            (btn3.getText().toString().equals(myString1[2]) )))
                            {
                                findViewById(R.id.imageView1).setVisibility(View.VISIBLE);
                            }



        }
    });

    Button next;
    next = (Button) findViewById(R.id.button5);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            startActivity(new Intent("com.learn.lone"));

        }
    });

}

}

我有一些button1, button2, button3如果点击时出现随机文本,

我有一个检查按钮,这个按钮用于检查上面按钮上的答案对错。如果正确答案将显示ImageView

如果出现我有问题ImageView,那么我无法点击button1,button2.button3。我只能按下一步继续。

我应该添加什么?

4

1 回答 1

2

是的,你可以满足你的条件而不是 if 循环使用这样的东西

button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
于 2012-06-15T06:11:38.013 回答