如果出现ImageView,如何禁用按钮?

时间:2012-06-15 05:44:49

标签: android button if-statement

我的禁用按钮有问题。

我的代码

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。我只能按下一步继续。

我应该添加什么?

1 个答案:

答案 0 :(得分:2)

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

button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);