Android setClickListner outside onCreate

2019-04-17 09:14发布

问题:

This is my onCreate function.

Since there is lot of code inside onclickListener for Hello i want to shift it outside to a method. how can i do that ?

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        facebook.authorize(this, permissions, Facebook.FORCE_DIALOG_AUTH,
                new DialogListener() {

                    public void onComplete(Bundle values) {
                    }

                    public void onFacebookError(FacebookError error) {
                    }

                    public void onError(DialogError e) {
                    }

                    public void onCancel() {

                    }
                });

        hello = (Button) findViewById(R.id.hello);
        info = (TextView) findViewById(R.id.facebook_info);
        content = (TextView) findViewById(R.id.content);

        hello.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                // bundle.putString("fields", "email");
                try {
                    about = facebook.request("me");
                    json = Util.parseJson(about);
                    id = json.getString("id");
                    first_name = json.getString("first_name");
                    last_name = json.getString("last_name");
                    email = json.getString("email");

                    if (!json.isNull("username")) {
                        username = json.getString("username");
                    } else {
                        Log.d("TAG", "Username not set");
                    }

                    dob = json.getString("birthday");
                    gender = json.getString("gender");
                    location = json.getString("location");

                    json_location = Util.parseJson(location);
                    place = json_location.getString("name");

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FacebookError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // info.setText(about);
                content.setText(id + " \n" + first_name + " \n" + last_name
                        + " \n" + email + " \n" + username + " \n" + dob
                        + " \n" + gender + " \n" + place);
            }
        });

    }

回答1:

Create an inner class extending OnClickListener.

private class MyOnClickListener implements OnClickListener { ... }

In here you add the exact same implementation as you all ready have for the anonymous OnClickListener. Just make sure that all variables you access are declared as local variables in your Activity-class.

Then when you set the onClickListener you just do this:

hello.setOnClickListener( new MyOnCLickListener() );


回答2:

 public class ClassName extends Activity implements OnClickListener {

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            facebook.authorize(this, permissions, Facebook.FORCE_DIALOG_AUTH,
                    new DialogListener() {

                        public void onComplete(Bundle values) {
                        }

                        public void onFacebookError(FacebookError error) {
                        }

                        public void onError(DialogError e) {
                        }

                        public void onCancel() {

                        }
                    });

            hello = (Button) findViewById(R.id.hello);
            hello.setOnClickListener(this);
            info = (TextView) findViewById(R.id.facebook_info);
            content = (TextView) findViewById(R.id.content);

        }

    public void onClick(View arg0) {
        if(arg0==hello){
                    try {
                        about = facebook.request("me");
                        json = Util.parseJson(about);
                        id = json.getString("id");
                        first_name = json.getString("first_name");
                        last_name = json.getString("last_name");
                        email = json.getString("email");

                        if (!json.isNull("username")) {
                            username = json.getString("username");
                        } else {
                            Log.d("TAG", "Username not set");
                        }

                        dob = json.getString("birthday");
                        gender = json.getString("gender");
                        location = json.getString("location");

                        json_location = Util.parseJson(location);
                        place = json_location.getString("name");

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (FacebookError e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // info.setText(about);
                    content.setText(id + " \n" + first_name + " \n" + last_name
                            + " \n" + email + " \n" + username + " \n" + dob
                            + " \n" + gender + " \n" + place);
                }
         }

    }

Replace your code & write above code for that.



回答3:

Place the code in a public / protected function. Since onClickListener is an abstract class inside yours, it can access public or protected methods from the class that nest it.