java - 如何从按钮获取文本来编辑文本?

标签 java android

this code work very well but now when question is CAR button1 = A button2 = R button3 = C when I click button3 button1 = INVISIBLE edit text = A and when I click button2 edit text = A I want when click on button edit text get text from button and check text in edit text

requires: edit text get char form button, add this code to function , remove last index from array

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import java.util.Random;


public class MainActivity extends AppCompatActivity {

   LinearLayout linearlayout;
   Button button;
   EditText txt;


    String Array[]= {"car","blue","red","fox"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        linearlayout = findViewById(R.id.linearlayout);
        txt = findViewById(R.id.txt);





        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';
            button = new Button(this);
            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) {


                    button.setVisibility(View.INVISIBLE);

                    txt.setText(txt.getText().toString() + button.getText());





                }
            });



        }


    }


    }

最佳答案

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import java.util.Random;


public class MainActivity extends AppCompatActivity {

    LinearLayout linearlayout;
    //Button button; //TODO: is local var!
    EditText txt; //Global var

    String Array[]= {"car","blue","red","fox"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearlayout = findViewById(R.id.linearlayout);
        txt = findViewById(R.id.txt);

        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);

                    txt.setText(txt.getText().toString() + button.getText());
                }
            });

        }

    }

}

关于java - 如何从按钮获取文本来编辑文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61593301/

相关文章:

android - 我们如何在android中获得电子邮件接收通知

java - 奇怪的日期格式转Java日期

c# - 取消全局变量?

java - java的赋值问题

Android LocationClient 类已弃用,但在文档中使用

android - 导入解析不起作用

Android MPEG DASH 客户端

java - 同步块(synchronized block)显示奇怪的结果

java - 将 hashmap 的 arraylist 放入另一个中

java - 如何将图像插入SqLite DB