3

我想显示 4 个数字选项,就像测验选项一样,四分之三应该是错误的,一个应该是正确答案。所有选项都是数字,每次打开页面时我都想更改正确答案的位置。请帮我

我的结果是一个字符串结果,我将在其中添加结果.....

我正在尝试这个,但没有得到解决方案

LinearLayout rowoptions = (LinearLayout) findViewById(R.id.linearlayout);
ArrayList<Integer> numbers = new ArrayList<Integer>();
String[] s = new String[4];
while (numbers.size() < 4) {
    int random = r.nextInt(10)+1;
    int chk = r.nextInt(4)+1;
    if (!numbers.contains(random)) {
        numbers.add(random);
        s[chk] =String.valueOf(random);
    }
}

for (int i = 0; i < 4; i++) {
    Button optionbutton = new Button(this);
    optionbutton.setText(s[i]);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70);
    layoutParams.setMargins(5, 5, 0, 0); // left, top, right, bottom
    optionbutton.setLayoutParams(layoutParams);
    //ivBowl.setBackgroundDrawable(null);

    rowoptions.addView(optionbutton);
}
4

1 回答 1

1

嗨,你需要这样的东西吗

LinearLayout layout=(LinearLayout)findViewById(R.id.mainlayout);
ArrayList list=new ArrayList();
    list.add(answer);
    Random r=new Random();
    for(int i=0;i<3;i++)
    {
        while(true)
        {
            int next=r.nextInt(10)+1;
            if(!list.contains(next))
            {
            list.add(next);
            break;
            }
        }
    }
    Collections.shuffle(list);
    for(int i=0;i<list.size();i++)
    {
        final Button b=new Button(this);
        b.setText(list.get(i).toString());

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int selectedAnser=Integer.parseInt(b.getText().toString());
                if(!(selectedAnser==answer))
                {
                    Toast.makeText(context, "Wrong Answer", Toast.LENGTH_SHORT).show();
                }

            }
        });
        layout.addView(b);
    }
于 2013-02-23T08:38:39.617 回答