java - 如何从按钮获取字符来编辑文本?
问题描述
这段代码工作得很好,它生成了三个按钮,因为我有三个元素“car”和随机元素 button_1“a”,button_2“r”,button_3“c”,我的问题是当我点击每个按钮时,它在编辑文本中的输入变成了“啊啊”
//the problem was here (var button)
edittext.setText(edittext.getText().toString() + button.getText());
我正在尝试让游戏猜词
String array[]= {"car"};
int random = (int) (Math.random() * array.clone().length);
StringBuilder word = new StringBuilder(array[random]);
for (int i = 0; i < array[random].length(); i++) {
char id = 'b';
final Button button = new Button(this); //local var
button.setId(id + i);
linearLayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//******the problem was here (var button)
button.setVisibility(View.INVISIBLE);
edittext.setText(edittext.getText().toString() + button.getText());
}
});
}
解决方案
推荐阅读
- r - 按月绘制前 n 个元素的最佳方法?
- embedded - CAN BUS 协议栈
- python - 带有 Python Lambda 的 AWS HTTP API
- android - 不要每次都创建 valueEventListener
- r - 排除 ggplot2 图例中的元素
- r - R :通过将 RGB 图像的红色和绿色值作为矩阵处理来将其设置为零
- python - 设计一种方法来从另一个数据帧的特定列构造一个新的数据帧,而无需可变输入参数
- dynamic-programming - 使用 C 查找 Krishnamurthy 数
- extjs - Modern 7.0 ComboBox 在每次值更改时触发“选择”事件
- java - 在 Android 中指定可见性