单击按钮时随机XML布局

时间:2015-04-09 10:58:31

标签: java android xml random

我有20个XML布局。我想要发生的是在单击按钮时显示随机xml布局。我尝试阅读same problem作为我的,但我没有工作。

例如,在Level1类中,当用户单击AlertDialog中的PositiveButton时,将打开随机XML布局(级别20或级别15而不是级别2)。

这是Level1类中的代码(相同的模式适用于其余类)



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
		




提前非常感谢你。任何代码片段都会有很大的帮助。

2 个答案:

答案 0 :(得分:0)

onClick()

内设置布局

如果您希望此代码位于上一个活动的onClick()方法中(如上所示),请使用以下代码:

@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);
}

答案 1 :(得分:0)

试试这个:

在onCreate之前声明一个public static int count = 0 .. 然后,在onClick中将计数递增1(计数++).. 使用switch语句,(当计数变为20时,别忘了将计数器重置为0)

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;
}

} 

而且,每次用户点击时,您都可以使用Math.random()并将其分配给count而不是递增计数;请记住检查(使用if语句)Math.random()返回一个值低于或等于20 ..)

希望它会帮助你..