如果在随机方法中使用一次,则不再使用文本

时间:2015-12-07 04:26:05

标签: android random textview

我通过Random方法使用string.xml中的随机文本到我的textview。现在,如果已经使用过一次,我不想再次使用该文本。将不胜感激。

我把文字放在string.xml

<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>

<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>

我在MainActivity中的代码:

Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;

number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
        R.string.eight,R.string.nine,R.string.ten};

array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
        R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
        R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};  

textview_number = number.nextInt(array_number.length - 1);
textView1.setText(getResources().getString(array_number[textview_number]));
textView2.setText(getResources().getString(array_number_explaination[textview_number]));

1 个答案:

答案 0 :(得分:0)

LinkedList<Integer>  number=new LinkedList<Integer>();
LinkedList<Integer>  number_exp=new LinkedList<Integer>();
//add data
int randomIndex=(int) (Math.random()*(strs.size()-1));
textview1.setText(number.get(randomIndex));
textview2.setText(number_exp.get(randomIndex));
number.remove(randomIndex);
number_exp.remove(randomIndex);