首页 > 解决方案 > 如何从按钮获取字符来编辑文本?

问题描述

这段代码工作得很好,它生成了三个按钮,因为我有三个元素“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());
            }
        });

    }

标签: javaandroid

解决方案


推荐阅读