简体   繁体   中英

How to get text from button to edit text?

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 = AI 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());
                }
            });

        }

    }

}

Not sure of the questions, but the one thing I did find to be an issue with the code.

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

The bolded part doesn't returns a char sequence. Should be the same thing as the edit text.

button.getText().toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM