gpt4 book ai didi

android - 在 android 中使用 charAt(index)

转载 作者:搜寻专家 更新时间:2023-11-01 09:41:00 24 4
gpt4 key购买 nike

我正在尝试将一个字符串分配给不同的按钮,以便使用 String.charAt(index) 方法将字符串中的每个字符分配给每个按钮。喜欢:

private String myString = "34567124";
/*Getting the Views*/
Button bUtton1 = (Button)findViewById(R.id.button1);
Button bUtton2 = (Button)findViewById(R.id.button2);
Button bUtton3 = (Button)findViewById(R.id.button3);
//...Rest of the Buttons

/*Setting text for each View*/
bUtton1.setText(myString.charAt(0));
bUtton2.setText(myString.charAt(1));
bUtton3.setText(myString.charAt(2));
...//Rest of the Buttons

为什么会导致错误?

最佳答案

在这种情况下,您可以考虑拆分 myString(这会将其转换为单字符字符串数组)。然后你可以这样做:

private String myString = "34567124";
//here I am splitting...
private String[] digitsArray = myString.split("");
...
//then you can do this:
bUtton1.setText(digitsArray[0]);
bUtton2.setText(digitsArray[1]);
bUtton3.setText(digitsArray[2]);

试一试,如果这符合您的预期,请告诉我。

关于android - 在 android 中使用 charAt(index),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39840690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com