DAS
DAS

Reputation: 696

How to populate the EditText with the button click?

I want to get the values in the button to be populated in the edit text box. i.e I am creating a login page in which the pin has to entered I have given a set of numbers as buttons, when I click the button the corresponding values has to be populated in the edit text box( just like the ATM action).

I am now using:

 b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View paramView) {
                // TODO Auto-generated method stub
                String text=(String) b1.getText();
                        pincode.setText(text);
            }
        });

with the above code I am getting the values populated, but I have a set of buttons, so is there any simplified way to get the values populated? *The values currently replacing the previous one , I need multiple values in the edit text like if we press buttons 1,2,3 (values) the values in the edit text should be 123 *

Upvotes: 2

Views: 6460

Answers (5)

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

onClick(View v)
{
    editText.setText(editText.getText() + v.getText());
    //plz adjust casting and spanned as per requirement
}

Upvotes: 1

MKJParekh
MKJParekh

Reputation: 34301

To do this, in your activity implements OnClickListener

then add this listener to all your button as

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);

and write onClick event as

public void onClick(View v) {

    Button b1 = (Button)v;
    editText.setText(editText.getText().toString()+b1.getText().toString());
}

and you are done :)

Upvotes: 1

Sathyapradeep
Sathyapradeep

Reputation: 163

on your button onClick you got to do this

button1.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"1");
});

button2.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"2");
});

this way your buttons will work

Upvotes: 1

user370305
user370305

Reputation: 109237

Button btnNO = (Button) findViewById(R.id.btn);
EditTExt edtText = (EditText) findViewById(R.id.editText);

btnNO.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
     String text= btnNo.getText();
     edtText.setText(text);

    // or direct use like, edtText.setText(btnNo.getText());
    }

});

Upvotes: 0

Gapipro
Gapipro

Reputation: 1973

You can add text like this:

String a = "random text";
input = your_edit_text_box;
input.setText(a);

Link: http://developer.android.com/reference/android/widget/EditText.html#setText%28java.lang.CharSequence,%20android.widget.TextView.BufferType%29

Upvotes: 0

Related Questions