1

I want to randomize my layout using this code:

public class testing extends Activity 
   {
    /** Called when the activity is first created. */
      private Integer [] mLinearLayoutIds = { 
            R.layout.games0,
            R.layout.games1,
            R.layout.games2,
            R.layout.games3,
            R.layout.games4,
            R.layout.games5,
            }; 
      public void onCreate(Bundle savedInstanceState) 
         {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
           Random random = new java.util.Random();
           int rand = random.nextInt(6);
           setContentView(mLinearLayoutIds[rand]);
         }
  }

but, every time the layout that was shown before is shown again.

How do I mark for a layout that has been shown before so it does not show again?

4

2 回答 2

2

这将需要持久存储。请参阅“SharedPreferences”来存储您的布局选项(或者如果您有很多,您可以选择使用 SQLite)。

每次用户启动活动时,您应该从数组中随机选择一个事件并将其存储为已使用并从该数组中取出。

这样做需要您在用户第一次打开应用程序时初始化数组。

(您可以只使用一个偏好并从包含您的选择的 JSONArray 中存储一个字符串。)

于 2012-10-08T22:43:11.103 回答
0

我假设“每次”是指“在后台后显示下一次活动”。我认为您已将代码放置在此任务的错误方法中。尝试将其移出void onCreate()to void onResume()

public class Testing extends Activity {
    private Integer [] mLinearLayoutIds = { 
        R.layout.games0,
        R.layout.games1,
        R.layout.games2,
        R.layout.games3,
        R.layout.games4,
        R.layout.games5,
    };

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
    }

    public void onResume() {
        Random random = new java.util.Random();
        int rand = random.nextInt(6);
        setContentView(mLinearLayoutIds[rand]);
    }
}
于 2012-10-09T17:55:16.360 回答