开发者

Do you need to inflate a view in order to get its id for the onClick method?

Do you need to inflate a view in order to get its id for the onClick method? Because when I run my program in the emulator and click on the specific button it does nothing! I want it to go back to the main.xml layout! Do I need to procces the onClick some other way?

public void onClick(View v) {
        switch( v.getId()){
            case R.id.play:
                setContentView(R.layout.play);
                setQuestion();
                break;
            case R.id.level:
                setContentView(R.layout.level);
                if(lvl.equals("1")) {
                    lvl1.setChecked(true);
                }
                if(lvl.equals("2")) {
                    lvl2.setChecked(true);
                }
                if(lvl.equals("3")) {
                    lvl3.setChecked(true);
                }
                if(lvl.equals("4")) {
                    lvl4.setChecked(true);
                }
                if(lvl.equals("5")) {
                    lvl5.setChecked(true);
                }
                 break;
            case R.id.setLevel:
                if(lvl1.isChecked()) {
                    setLevel("1");
                }
                if(lvl2.isChecked()) {
                    setLevel("2");

                }
                if(lvl3.isChecked()) {
                    setLevel("3");
                }
                if(lvl4.isChecked()) {
                    setLevel("4");
                }
                if(lvl5.isChecked()) {
                    setLevel("5");
                }
                setContentView(R.layout.main);
                break;
        }
    }

Here is how I get the views:

setContentView(R.layout.main);

        Button play = (Button)findViewById(R.id.play);
        play.setOnClickListener(this);
        Button level = (Button)findViewById(R.id开发者_如何学C.level);
        level.setOnClickListener(this);
        Button setLevel = (Button)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.setLevel);

        setLevel.setOnClickListener(this);


        lvl1 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl1);

        lvl2 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl2);

        lvl3 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl3);

        lvl4 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl4);

        lvl5 = (RadioButton)getLayoutInflater().inflate(R.layout.level, null).findViewById(R.id.lvl5);

What should I do so that when I click the setLevel button it changes the view back to the main.xml view


Andrew,

You may want to make individual onClick() methods for each item. That's how I handle my click-able objects.

Example:

Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //perform play button actions here
            }
        });

This way you already have the button object created based on its ID value, and the onClickListener is specifically tailored to that item.

good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜