简体   繁体   English

Android Studio-如何在随机选择的字符串上创建后退按钮?

[英]Android Studio - How to create a back button on randomly selected strings?

I have an array of strings that I am displaying in my activity using a random number every time a button called Next is clicked. 每次单击名为“ 下一步”的按钮时,我都会使用一个随机数在活动中显示一个字符串数组。 How do I create another button called Back , that simply displays the previously or last generated strings in the order they were randomly generated?. 如何创建另一个名为Back的按钮,该按钮仅按随机生成的顺序显示先前或最后生成的字符串? So that instead of clicking next to generate another string, I can click Back at any time to view all the previous strings one by one and then click Next again to continue the random selection. 这样一来,无需单击下一步以生成另一个字符串,而是可以随时单击“上一步”以逐个查看所有先前的字符串,然后再次单击“ 下一步”以继续进行随机选择。

Create a array list of String like 创建一个类似String的数组列表

ArrayList<String> arraylist= new ArrayList<String>();

after that using add function add the strings that you are displaying in your activity using a random number every time a button called Next is clicked. 之后,使用add函数每次单击名为Next的按钮时,都会使用一个随机数添加您在活动中显示的字符串。

 arraylist.add("your_randomly_generated_string");

now your randomly generated string will stored in arraylist one by one This will store the string now how to retrieve? 现在,您随机生成的字符串将一一存储在arraylist中。现在将如何存储字符串? following steps may help you not sure ... While adding the randomly generated string into arraylist initialize the int variable with zero and increment its value after each adding... 以下步骤可能会帮助您不确定...将随机生成的字符串添加到arraylist时,将int变量初始化为零,并在每次添加后递增其值...

public int indexvalue=0;

on your Onclick of NEXT button add two lines 在您的NEXT按钮的Onclick上添加两行

arraylist.add("your_randomly_generated_string");
indexvalue++;

And now on the Onclick of BACK Button Use indexvalue to retrieve... 现在,在“返回”按钮的Onclick上,使用indexvalue检索...

String data = arraylist.get(indexvalue);
indexvalue--;

here Decremented the indexvalue to retrieve the previous string.. hope you'll understand what im trying to say little bit of lengthy explanation ... 在这里递减indexvalue来检索先前的字符串。.希望您能理解我试图说些冗长的解释...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM