停止显示之前的随机活动

时间:2016-05-22 02:12:04

标签: java android random

有谁知道如何完全阻止随机活动显示?我有5节课,每节课10个问题。一旦给出正确的答案,我想继续下一个随机课程,而不是让上一课程再次出现。一旦第一次回答了所有随机类,则Random方法结束。我尝试了finish(),但这不起作用。按下按钮后,我仍然会在已经回答后重复上课。请查看我的代码,并告知是否可以。 THX

public class HomeActivity扩展了AppCompatActivity {

TextView myTv;
Button nextBtn;
EditText myEt;
Button playgameBtn;

int mCounter = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    myTv = (TextView) findViewById(R.id.myTv);
    nextBtn = (Button) findViewById(R.id.nextBtn);
    playgameBtn = (Button) findViewById(R.id.playgameBtn);
    myEt = (EditText) findViewById(R.id.myEt);


   final String[] clazzes = new String[] {"Mark.class", "John.class", "Caleb.class", "Isaiah.class", "Jacob.class"};


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

            ArrayList<Class> activityList = new ArrayList<>();
            activityList.add(Mark.class);
            activityList.add(John.class);
            activityList.add(Caleb.class);
            activityList.add(Isaiah.class);
            activityList.add(Jacob.class);


            Random generator = new Random();
            int number = generator.nextInt(5)*1;

            Class activity = null;

            switch (number){
                case 1:
                    activity = John.class;
                    break;
                case 2:
                    activity = Caleb.class;
                    break;
                case 3:
                    activity = Mark.class;
                    break;
                case 4:
                    activity = Isaiah.class;
                    break;
                default:
                    activity = Jacob.class;
                    break;


            }
            Intent intent = new Intent(getBaseContext(), activity);



             startActivity(intent);
            finish();

            }

private void finish() {
}


    });


      }



  }

3 个答案:

答案 0 :(得分:0)

这不是编程的好方法。您的解决方案可以是:从代码中删除该空结束函数。

public class HomeActivity extends AppCompatActivity {
TextView myTv;
Button nextBtn;
EditText myEt;
Button playgameBtn;

int mCounter = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

myTv = (TextView) findViewById(R.id.myTv);
nextBtn = (Button) findViewById(R.id.nextBtn);
playgameBtn = (Button) findViewById(R.id.playgameBtn);
myEt = (EditText) findViewById(R.id.myEt);


final String[] clazzes = new String[] {"Mark.class", "John.class", "Caleb.class", "Isaiah.class", "Jacob.class"};
playgameBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        ArrayList<Class> activityList = new ArrayList<>();
        activityList.add(Mark.class);
        activityList.add(John.class);
        activityList.add(Caleb.class);
        activityList.add(Isaiah.class);
        activityList.add(Jacob.class);


        Random generator = new Random();
        int number = generator.nextInt(5)*1;

        Class activity = null;

        switch (number){
            case 1:
                activity = John.class;
                break;
            case 2:
                activity = Caleb.class;
                break;
            case 3:
                activity = Mark.class;
                break;
            case 4:
                activity = Isaiah.class;
                break;
            default:
                activity = Jacob.class;
                break;


        }
        Intent intent = new Intent(getBaseContext(), activity);



         startActivity(intent);
        finish();

        }

});


  }



}

答案 1 :(得分:0)

一种方法是使用Collections.shuffle()来随机化项目,然后在调用活动后“弹出”列表的最后一项。

        ArrayList<Class> activityList = new ArrayList<>();
        activityList.add(Mark.class);
        activityList.add(John.class);
        activityList.add(Caleb.class);
        activityList.add(Isaiah.class);
        activityList.add(Jacob.class);
        Collections.shuffle(activityList);

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


       Intent intent = new Intent(getBaseContext(), activityList.get(0));
        startActivity(intent);
        activityList.remove(0);

        }

        [...]

请记住,您应该将对象添加到setOnClickListener方法

之外的ArrayList中

答案 2 :(得分:0)

由于你不想使用通常做这种事情的推荐方式的片段,我会给你一个非常基本的例子,除了你已经提供的内容。

//make a list as an instance variable
private static List<Integer> displayedClasses;

//In your onClick event get the random number as follows
int number=getRandomNumber();
//getRandomNumber() will return -1 if all classes are displayed. Handle it here.
if(number==-1){
   displayedClasses.clear();
   //do something else
   return;
}
//store the number in the above list
if(displayedClasses==null)displayedClasses=new ArrayList<>();
displayedClasses.add(new Integer(number));

//continue with the rest of the statement

getRandomNumberMethod

private int getRandomNumber(){
    if(l.size()>=5) {
       return -1;
    }
    Random random=new Random();
    int num=0;
    while(l.contains(num)){
        num=random.nextInt(5)*1;
    }
    return num;
}