外面的onCreate的Andr​​oid setClickListner [英] Android setClickListner outside onCreate

查看:178
本文介绍了外面的onCreate的Andr​​oid setClickListner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的onCreate功能。

由于有很多业内onclickListener code为你好,我想外面转移它的方法。我该怎么办呢?

 公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        facebook.authorize(这一点,权限,Facebook.FORCE_DIALOG_AUTH,
                新DialogListener(){                    公共无效的onComplete(束值){
                    }                    公共无效onFacebookError(FacebookError错误){
                    }                    公共无效onerror的(DialogError E){
                    }                    公共无效onCancel(){                    }
                });        你好=(按钮)findViewById(R.id.hello);
        信息=(的TextView)findViewById(R.id.facebook_info);
        内容=(的TextView)findViewById(R.id.content);        hello.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看为arg0){
                // bundle.putString(域,电子邮件);
                尝试{
                    关于= facebook.request(我);
                    JSON = Util.parseJson(约);
                    ID = json.getString(ID);
                    FIRST_NAME = json.getString(FIRST_NAME);
                    姓氏= json.getString(姓氏);
                    电子邮件= json.getString(电子邮件);                    如果(!json.isNull(用户名)){
                        用户名= json.getString(用户名);
                    }其他{
                        Log.d(TAG,用户名未设置);
                    }                    DOB = json.getString(生日);
                    性别= json.getString(性别);
                    位置= json.getString(位置);                    json_location = Util.parseJson(位置);
                    地方= json_location.getString(名称);                }赶上(MalformedURLException的E){
                    e.printStackTrace();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }赶上(JSONException E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(FacebookError E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }                // info.setText(约);
                content.setText(ID +\\ n+ FIRST_NAME +\\ n+姓氏
                        +\\ n+电子邮件+\\ n+用户名+\\ n+ DOB
                        +\\ n+性别+\\ n+地方);
            }
        });    }


解决方案

创建内部类扩展OnClickListener。

 私有类MyOnClickListener实现OnClickListener {...}

在这里,你添加完全相同的执行情况你们都准备好了吗有匿名OnClickListener。只要确保所有的变量您的访问被声明为在活动级的局部变量。

然后,当你设置onClickListener你只是这样做:

  hello.setOnClickListener(新MyOnCLickListener());

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);
            }
        });

    }

解决方案

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() );

这篇关于外面的onCreate的Andr​​oid setClickListner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆