简体   繁体   中英

Get View.OnClickListener from other class?

i'm pretty new in Android and i'm stuck with handling OnClickListeners from separate class

In my Main Main_Activity i have a lot of buttons and View.OnClickListeners to handle them . OnClickListeners looks messy in code, so i decided to move them in separate class where they will be helded. And right after that I stuck with problem that Main_Activity doesn't want to apply them from separated class .

I've tried to create method in ListenersHolder class ,that set designated listener that but this don't work because non-static method cannot be referenced from a static context:

 //code for separate class that helds listeners 
public  abstract class Listeners_Utils extends Main_Activity implements View.OnClickListener {

private final View.OnClickListener listener;
private final Main_Activity main;

public Listeners_Utils (Main_Activity main, View.OnClickListener listener) {
    this.listener = listener;
    this.main = main;
}

 //listners itself
View.OnClickListener button1Listener = new View.OnClickListener() {
@Override
public void onClick(View view ) {
   /some code    }
};

View.OnClickListener button2Listener = new View.OnClickListener() {
@Override
public void onClick(View view ) {
   /some code    }
};
.... 
View.OnClickListener buttonNListener = new View.OnClickListener() {
@Override
public void onClick(View view ) {
   /some code    }
};

method to get listener depends on that String will be printed

public View.OnClickListener getListener(String listenerName) {
    switch (listenerName) {
        case "button1":
            massButton.setOnClickListener(massListener);
            break;
        case "button2":
            lengthButton.setOnClickListener(lengthListener);
            break;
 return listener;

In my activity i have something like this, but it doesn't work

Button1.setOnClickListener(Listeners_Utils.getListener(button1);
Button2.setOnClickListener(Listeners_Utils.getListener(button2);
 ....
ButtonN.setOnClickListener(Listeners_Utils.getListener(buttonN);

So question is:
What is proper way to get listeners from Listeners_Utils.class in Main_Activity.class and apply them for specified button?

Thanks for considering my question!

I would suggest you use Android Data Binding where you would be able to separate your logic from view. This way, all your listeners will be handled in a different class separate from and Activity or Fragment class. Below is a link to the official documentation by Google on data binding https://developer.android.com/topic/libraries/data-binding/expressions#event_handling

Usually it's simpler for the activity to be handling its views on its own, but assuming you want to go your way something like this can work:

// Should not extend MainActivity 
public  class ListenersUtils {

    public static View.OnClickListener getListener(final MainActivity main, String listenerName) {
        switch (listenerName) {
            case "button1":
                return new View.OnClickListener() {
                    @Override
                    public void onClick(View view ) {
                        //some code that can reference MainActivity
                    }
                };
            default:
                break;
        }
        return null;
    }
}

And in your MainActivity:

button1.setOnClickListener(ListenersUtils.getListener("button1");

I don't understand what your goal is to make the abstract class for onClicklistener. But to answer how to get specific buttons in an activity, you can do it like this.

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;

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

        btn1 = findViewById(R.id.my_button);
        btn2 = findViewById(R.id.my_button2);

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


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.my_button:
                Toast.makeText(this, "Ok", Toast.LENGTH_SHORT).show();
                break;

            case R.id.my_button2:
                Toast.makeText(this, "Ok2", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

The other solutions are good too, but if you want to stay in the same activity , you could also use this method If you don't want to create

button1Listener = new View.OnClickListener() {
 @Override
 public void onClick(View view ) {
 /some code    
     }
 }; 

You could use the attribute onClick in the XML file for the button.

For example : IN XML FILE

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

In MainActivity : define the functionality you want to be performed when the button is clicked

 public void createToast(View view) {
     Toast.makeText(MainActivity.this , "Clicked on the button " , Toast.LENGTH_SHORT).show() ; 
}

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