【问题标题】:Android setClickListner outside onCreateAndroid setClickListner 外 onCreate
【发布时间】:2011-09-12 15:45:02
【问题描述】:

这是我的 onCreate 函数。

由于在 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);
        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);
            }
        });

    }

【问题讨论】:

    标签: android clicklistener


    【解决方案1】:

    创建一个扩展 OnClickListener 的内部类。

    private class MyOnClickListener implements OnClickListener { ... }
    

    在这里,您添加与匿名 OnClickListener 完全相同的实现。只需确保您访问的所有变量都在 Activity 类中声明为局部变量。

    然后,当您设置 onClickListener 时,您只需执行以下操作:

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

      替换您的代码并为此编写上面的代码。

      【讨论】:

        【解决方案3】:

        将代码放在公共/受保护的函数中。由于 onClickListener 是你内部的一个抽象类,它可以从嵌套它的类中访问公共或受保护的方法。

        【讨论】:

          猜你喜欢
          • 2014-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多