How to pick any button through Onclick?

Asked

Viewed 422 times

1

I have ten Buttons in my XML and I want you to change the Text of the button that the user click.

I implemented a View.Onclicklistener in my Activity and my onClick was like this:

@Override
    public void onClick(View view) {
        Button button = (Button) view;
        button.setText("X");
    }

It didn’t work out. Am I required to create a switch for each Button? There is no way to use a single code for all buttons?

  • See if this Soen response helps: https://stackoverflow.com/a/40282194/1377664

  • According to the link above I am obliged to create a Setonclicklistener for each button or check one by one through id.

  • Can you specify what went wrong? Because what you did works yes.

3 answers

0

In your XML you can use the attribute onClick so that they all make a call to the click method.

XML

<Button
    android:id="@+id/buttonOne"
    ...
    android:onClick="onButtonClick"/>

<Button
    android:id="@+id/buttonTwo"
    ...
    android:onClick="onButtonClick"/>

Activity

public void onButtonClick(View view) {
    switch (view.getId()) {

        case R.id.buttonOne: { break; }
        case R.id.buttonTwo: { break; }
        case R.id.buttonThree: { break; }
        ...
    }
}

This way you can save some lines of code. Another thing, depending on what functions the buttons on your screen have, you can also use a Recyclerview, which will also save you a lot of code. But this will depend on the function exercised by each button, if they are very different, I believe you want to use the example of the same code, but if they arrive in the same end, you could use a Recylerview.

For example, if you want to open a screen for each button, you could use the RV and depending on the button clicked, return a Bundle to open the corresponding screen, and so on.

0

Look, I was pretty sure that this is something that is possible to do yes. I got that flea behind my ear, and I decided to test it quickly. The result was that both buttons were left with the text "Clicked!" when clicked.

My XML

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button"
            android:id="@+id/button"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button2"
            android:id="@+id/button2"
            android:layout_below="@+id/button"
            android:layout_alignParentStart="true" />

Na Activity

this.button.setOnClickListener(this)
this.button2.setOnClickListener(this)

 override fun onClick(v: View?) {
    var button = v as Button
    button.text = "Clicked!"
  }

0

Implement View.Onclicklistener in your Activity, in the onClick method do it

Remember to set the buttons

Button Bt = (Button) findViewById (R.id.button); Bt.setOnClickListener (this);

Public void onClick (View view) { Button tmp = (Button) findViewById (view.getId ()); tmp.setText ("Funciona");}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.