如何在单击按钮时进行随机布局

时间:2015-08-30 06:54:18

标签: java android android-layout random

其实我想让它成为随机类,但后来我认为很多活动可以让应用程序变慢所以我只想让相对布局随机

所以我在一个活动类中有5个布局

layout1 = (RelativeLayout)findViewById(R.id.layout1);
layout2 = (RelativeLayout)findViewById(R.id.layout2);
layout3 = (RelativeLayout)findViewById(R.id.layout3);
layout4 = (RelativeLayout)findViewById(R.id.layout4);  
layout5 = (RelativeLayout)findViewById(R.id.layout5);

并且在每个布局中都有一个按钮,使布局随机再次

button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v){ 

        //The code to how make layout random

        }
     });
 }

然后如果按下随机按钮,如何使已经打开的布局不再打开?然后如果所有布局都已打开,它将打开新的活动类

任何人都可以帮我解释一下这个代码吗?

3 个答案:

答案 0 :(得分:3)

最初将可见性设置为所有相对布局,并将它们全部放入View的ArrayList中。

获取0到列表大小的随机数。

在随机位置获取View并将其可见性设置为Visible并从ArrayList中删除。

在ArrayList为空之前做同样的事情。

当ArrayList为空时创建新活动。

代码:

ArrayList<View> viewList=new ArrayList<>();
initLayouts(){
    layout1 = (RelativeLayout)findViewById(R.id.layout1);
    layout2 = (RelativeLayout)findViewById(R.id.layout2);
    layout3 = (RelativeLayout)findViewById(R.id.layout3);
    layout4 = (RelativeLayout)findViewById(R.id.layout4);
    layout5 = (RelativeLayout)findViewById(R.id.layout5);


    viewList.add(layout1);
    viewList.add(layout2);
    viewList.add(layout3);
    viewList.add(layout4);
    viewList.add(layout5);

    for(int i=0;i<viewList.size();i++){
        viewList.get(i).setVisibility(View.GONE);
    }

button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v){
        loadRandomLayout();
    }
});
}
public loadRandomLayout(){
    if(viewList.size()>0) {
        Random r = new Random();
        int number = r.nextInt(viewList.size());
        viewList.get(number).setVisibility(View.VISIBLE);
        viewList.remove(number);
    }else{
        startActivity(new Intent(this,NewActivity.class));
    }
}

答案 1 :(得分:2)

您可以按如下方式创建随机int:

//To get a Random number 1-5 (I saw your RelativeLayouts and you've 5
Random rand = new Random();
int randomNum = rand.nextInt((5 - 1) + 1) + 1;

然后你可以创建一个方法来选择要显示的内容:

public void ShowRelativeLayout(int rand){

 switch(rand){
  case 1:
    if (layout1.getVisibility() == View.VISIBLE) {
     //Do nothing cause it's visible
     break;
 } else {
     layout1.setVisibility(View.VISIBLE);
     break;

 }
 case 2:
 ..........

}

答案 2 :(得分:2)

创建一个数组来存储布局索引。

RelativeLayout[] layout = new RelativeLayout[5];
layout[0] = (RelativeLayout)findViewById(R.id.layout[0]); // 0
layout[1] = (RelativeLayout)findViewById(R.id.layout[1]); // 1
layout[2] = (RelativeLayout)findViewById(R.id.layout[2]); // 2
layout[3] = (RelativeLayout)findViewById(R.id.layout[3]); // 3
layout[4] = (RelativeLayout)findViewById(R.id.layout[4]); // 4

制作一个简单的随机数生成器。

public void FindNewLayout ()
{

  Random r_generator = new Random();

  int randomNum;

  //now the only way to know which layouts have been shown before, you 
  //need to store the indexes that have been used before, somewhere. 
  //I recommend using an array.

  // count, and the array below should be initialized somewhere else 
  //rather than inside the method so that only one instance of each is 
  //created, but for simplicity I'll just put their initialization here
  int static count = 0; 
  //I'll explain below what count does.

  // the log array that remembers each layout change
  boolean[] log = new boolean[5];

  do 
  {
    //select new random number
    randomNum = r_generator.nextInt((max - min) + 1) + min;
    //in this case max = 4, min = 0, so replace these values in the 
    //equation above

    // check the log to see if the number has appeared again
    if ( log[randomNum] == false )
    {
      //Great! it hasn't appeared before, so change layout
      log[randomNum] = true;
      layout[randomNum].setVisibility = true;
      count++; // increases step
      break; //stops while because an unused layout has been found
    }

  }while (count<5)
  //if the value of count is equal to 5 then every layout has been used 
  //before so the do-while code should not be run again

}// end method

每当您想尝试更改布局时,都应调用上述方法。

最后,您可以使用类似Debugger.log("message");语句的内容 如果需要,可以在控制台上打印以进行调试,以便找出布局何时发生变化。