简体   繁体   中英

Random XML Layout on button click

I have 20 XML layouts. What I want to happen is to show random xml layouts when the button is clicked. I tried and read same problem as mine but i didnt worked.

For example in Level1 class when the user clicked the PositiveButton in the AlertDialog, random XML Layout will be opened (Level 20 or Level 15 not Level 2).

This is code in Level1 class(the same pattern applies for the rest of the classes)

 public class Luzon1 extends Activity { private String [] answers; private Button answerButton; private TextView scoreTxt, showClue; private EditText answerText; int scoreNew=0; public Button yes; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_luzon1); } public void init() { //correct answer answers=new String[]{"Tarlac"}; (R.id.AnswerButton); answerButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub checkAnswer(); } }); } public boolean isCorrect(String answer) { return(answer.equalsIgnoreCase(answers[currentQuestion])); } public void checkAnswer() { String answer=answerText.getText().toString(); if(isCorrect(answer)) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Congratulations! You will receive P10!"); builder.setMessage("Did you know that Former bla bla bla Did you know that Former bla bla bla Did you know that Former bla bla bla"); builder.setIcon(android.R.drawable.btn_star_big_on); builder.setPositiveButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String userData=scoreTxt.getText().toString(); int userNumber=Integer.parseInt(userData); Intent intent=new Intent(Luzon1.this, Luzon2.class); intent.putExtra("parameter name", userNumber); startActivity(intent); Luzon1.this.finish(); System.exit(0); } }); AlertDialog alert = builder.create(); alert.show(); // Show Alert Dialog 

Thank you so much in advance. and any code snippet will be a great help.

Setting the layout from inside onClick()

If you want this code to be in the onClick() method in your previous activity (the one shown above), use the following code:

@Override
protected void onClick(DialogInterface dialog, int which) {
    Random generator = new Random(); 
    int number = generator.nextInt(NUMBER_OF_LEVELS) + 1;

    Class activity;

    switch(number) {
        case 1:
            activity = LevelOne.class;
            break;
        case 2:
            activity = LevelTwo.class;
            break;
        case 3:
            activity = LevelThree.class;
            break;
        case 4:
            activity = LevelFour.class;
            break;
        case 5:
            activity = LevelFive.class;
            break;

        ...

        case 20:
            activity = LevelTwenty.class;
            break;
    }

    Intent intent = (getBaseContext(), activity);
    startActivity(intent);
}

Try this:

Declare a public static int count = 0 before onCreate.. then, In onClick increment the count by 1(count++).. Use switch statement like,(Dont forget to reset counter to 0 when count becomes 20)

void onClick(){
      count++;

      switch(count) {
             case 1:
                   setContentView(R.layout.yourLayout1);
                   break;
             case 2:

                   setContentView(R.layout.yourLayout2);
                   break;
             case 3:
                   setContentView(R.layout.yourLayout3);
                   break;                 
             case 4:
                   setContentView(R.layout.yourLayout4);
                      ................................
                      ................................
             case 20:
                   setContentView(R.layout.yourLayout20);
                   break;                     
            }
      if(count==20){
          count = 0;
}

} 

And also instead of incrementing count each time user clicks, you can use Math.random() and assign it to count (Remember to check(use if statement) Math.random() is returning a value which is below or equal to 20..)

Hope it will help you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM