【问题标题】:How can I clear random EditTexts?如何清除随机 EditTexts?
【发布时间】:2020-01-22 03:47:16
【问题描述】:

我有九个显示随机数的 EditText。

generateSquares 方法将 EditTexts 中编号为 1 到 9 的文本随机设置为无序列表。

然后我想在程序启动时清除一些EditTexts,在onCreate方法中。

我想随机清除 9 个 EditText 中的 2、3、4 或 5 个。

一旦我启动程序,我想随机清除两个editTexts。

那些将是 EditText1+EditText4 或

EditText2+9;或

EditText3+EditText6;

如果算法清除 3 EditText 那些将是

例如EditText1+EditText4+EditText9。

我需要的是在EditTexts设置Text之后,随机清除2、3、4或5个EditTexts

private EditText e1;
private EditText e2;
private EditText e3;
private EditText e4;
private EditText e5;
private EditText e6;
private EditText e7;
private EditText e8;
private EditText e9;

//Set Text of EditTexts randomly (numbers from 1 to 9)

public void generateSquares() {


    // Generates a random array to SetText of EditTexts randomly
    Integer [] shuffleArray = new Integer[] { 1,2,3,4,5,6,7,8,9 };
    List<Integer> shuffleList = Arrays.asList(shuffleArray);
    Collections.shuffle(shuffleList);
    listaDesordenada.toArray(shuffleList);

    String arrayItem1 = Integer.toString(shuffleArray[0]);
    String arrayItem2 = Integer.toString(shuffleArray[1]);
    String arrayItem3 = Integer.toString(shuffleArray[2]);
    String arrayItem4 = Integer.toString(shuffleArray[3]);
    String arrayItem5 = Integer.toString(shuffleArray[4]);
    String arrayItem6 = Integer.toString(shuffleArray[5]);
    String arrayItem7 = Integer.toString(shuffleArray[6]);
    String arrayItem8 = Integer.toString(shuffleArray[7]);
    String arrayItem9 = Integer.toString(shuffleArray[8]);


    t1.setText(arrayItem1);
    t2.setText(arrayItem2);
    t3.setText(arrayItem3);
    t4.setText(arrayItem4);
    t5.setText(arrayItem5);
    t6.setText(arrayItem6);
    t7.setText(arrayItem7);
    t8.setText(arrayItem8);
    t9.setText(arrayItem9);

}

 //Clear randomly EditTexts:

?

【问题讨论】:

    标签: android-studio android-edittext


    【解决方案1】:

    好吧,为了简单起见,您可以生成随机的 int maxCount,它会告诉您有多少 EditText 将被清除。在下一步(例如循环)中,您将随机生成 int index,它将告诉您要清除哪个索引(来自先前生成的数组)。您可以将清除的索引存储在某个数组中,以防止多次清除同一索引。这对你来说够了吗?

    【讨论】:

    • 很棒的 qki,所以我需要将 EditTexts 存储在某个数组中以使用索引删除,现在我会阅读是否可以编写一个 EditTexts 数组,谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2016-07-23
    • 1970-01-01
    • 2014-04-15
    • 2011-10-28
    • 2018-04-03
    • 2014-05-06
    • 2016-12-12
    相关资源
    最近更新 更多