How to handle button clicks using the XML onClick within Fragments

前端 未结 18 1764
旧时难觅i
旧时难觅i 2020-11-22 01:41

Pre-Honeycomb (Android 3), each Activity was registered to handle button clicks via the onClick tag in a Layout\'s XML:

android:onClick=\"m         


        
18条回答
  •  日久生厌
    2020-11-22 02:10

    The problem I think is that the view is still the activity, not the fragment. The fragments doesn't have any independent view of its own and is attached to the parent activities view. Thats why the event ends up in the Activity, not the fragment. Its unfortunate, but I think you will need some code to make this work.

    What I've been doing during conversions is simply adding a click listener that calls the old event handler.

    for instance:

    final Button loginButton = (Button) view.findViewById(R.id.loginButton);
    loginButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            onLoginClicked(v);
        }
    });
    

提交回复
热议问题