skip to Main Content

When I click on my button, it is supposed to "put the current String Text of the button" into the EditText.

But clicking on a new button replaces the existing string value in the EditText while I want it to not remove the previous input.

Ex. "When I click on a Button with the Text String of "1" in it, it appears on the editText. When I click on another Button with a different String, it replaces the current value in the EditText while I want them to stay.

What my current layout looks like (Sorry for the choice colors)

private void LoadBtnNumbers(){
        Button[] btn = new Button[] {

                (Button) findViewById(R.id.btn1),
                (Button) findViewById(R.id.btn2),
                (Button) findViewById(R.id.btn3),
                (Button) findViewById(R.id.btn4),
                (Button) findViewById(R.id.btn5),
                (Button) findViewById(R.id.btn6),
                (Button) findViewById(R.id.btn7),
                (Button) findViewById(R.id.btn8),
                (Button) findViewById(R.id.btn9),
        };

        ArrayList<String> array = new ArrayList<>();
        array.add("1");
        array.add("2");
        array.add("3");
        array.add("4");
        array.add("5");
        array.add("6");
        array.add("7");
        array.add("8");
        array.add("9");


        for(int i = 0; i < btn.length; i++) {

            Random rand = new Random();
            String newString = array.get(rand.nextInt(array.size()));
            btn[i].setText(newString);

            array.remove(newString);
            String text = btn[i].getText().toString();

            btn[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("BTN","NUMBER = "+ text);
                    txtPass.setText(text);
                }
            });
        }
    }

My code above is to get an Array of Buttons and ArrayList of Strings, to then set the text of the button randomly on start from the string Array.

Do let me know if what I imagine is even possible and how can I accomplished that.

Thank You.

2

Answers


  1. Try like this

    btn[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String btnTextValue = ((Button) v).getText().toStrinh();
                    txtPass.setText(btnTextValue);
                }
            });
    
    Login or Signup to reply.
  2. If you want to append the button text to the current EditText string instead of replacing it, you should get the current string first, append the new value, then put the new appended string back in the EditText, like this:

    btn[i].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String newText = txtPass.getText().toString() + text;
            txtPass.setText(newText);
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search