【问题标题】:How to make a Random Layout when button clicked单击按钮时如何进行随机布局
【发布时间】:2015-08-30 06:54:17
【问题描述】:

实际上我想将其设为 Random 类,但我认为过多的活动会使应用程序变慢,所以我只想将相对布局设为 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

        }
     });
 }

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

谁能帮我解释一下,给出一些示例代码?

【问题讨论】:

  • 如何打开一个新的RelativeLayout
  • 看看我的骨架

标签: java android android-layout random


【解决方案1】:

最初设置所有相关布局的可见性,并将它们全部放入 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.7以下的源级别不允许使用''运算符
  • 替换为 .
  • 如果我替换 仍然会出错,但如果我删除 它就完成了
【解决方案2】:

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

//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:
 ..........

}

【讨论】:

  • 但是之前可见的布局又被打开了
  • 不,因为你问有随机数的布局是否可见,不要什么都不做,不是你想要的吗?
【解决方案3】:

制作一个数组来存储布局索引。

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"); 的语句 如果需要,可以在控制台上打印以进行调试,以便了解布局何时发生更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多