显示后不要重复(随机)

时间:2016-06-27 16:50:46

标签: android random

按下它时会显示一个按钮,显示随机文本。 我有另一个按钮,按下它将删除案件,因此它不会再显示。怎么做? 这是我的代码:

    public class TasksActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_tasks);

    final Button tasksbtn = (Button)findViewById(R.id.btnfortasks);
    Button removeCase = (Button)findViewById(R.id.remove_case); 


    tasksbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Random taskRandom = new Random();
            TextView tasksView = (TextView)findViewById(R.id.tasks_textView);
            switch (taskRandom.nextInt() %4) {
                case 0:
                    tasksView.setText("one");
                    break;
                case 1:
                    tasksView.setText("two");
                    break;
                case 2:
                    tasksView.setText("three");
                    break;
                case 3:
                    tasksView.setText("four");
                    break;
                default:
                    break;
            }

        }
    });

2 个答案:

答案 0 :(得分:0)

你可以这样做:

public class TasksActivity extends AppCompatActivity {

    private static ArrayList<String> list;

    //REST OF YOUR CODE

    switch (taskRandom.nextInt() %4) {
            case 0:
                if (list.contains("one")) {
                    break;
                }
                tasksView.setText("one");
                list.add("one");
                break;

            //CONTINUE FOR THE REST OF CASES
    }

答案 1 :(得分:0)

不是Android程序员,所以我不会尝试接口的东西。但是,以下内容可以让您了解如何使用Collections.shuffle()

执行此操作
import java.util.ArrayList;
import java.util.Collections;

public class NoRepeats {

   private static String[] labels = {"one", "two", "three", "four", "five"};
   private static ArrayList<Integer> indices = new ArrayList<Integer>();
   private static int idx = -1;

   /*
    * Produces no repeats until the labels have been exhausted, then sticks.
    */
   public static void pushSecondButton() {
      /*
       * To change behavior so it generates new blocks of unrepeated
       * labels after exhausting the list, replace the first 'if'
       * with:
       *         idx = (idx + 1) % labels.length;
       */
      if (idx < labels.length - 1) {
         ++idx;
      }
      if (idx == 0) {
         Collections.shuffle(indices);
      }
   }

   public static void main(String[] args) {
      for (int i = 0; i < labels.length; ++i) {
         indices.add(i);
      }

      // fake 7 button pushes to change the label...
      for (int i = 0; i < 7; ++i) {
         pushSecondButton();
         System.out.println(labels[indices.get(idx)]);
      }
   }

}