这是我的onCreate功能。
由于onclickListener中有很多代码用于Hello,我想将它移到一个方法之外。我怎样才能做到这一点 ?
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);
}
});
}
答案 0 :(得分:2)
创建一个扩展OnClickListener的内部类。
private class MyOnClickListener implements OnClickListener { ... }
在这里,您可以为匿名OnClickListener添加完全相同的实现。只需确保您访问的所有变量都在Activity类中声明为局部变量。
然后当您设置onClickListener时,您只需执行此操作:
hello.setOnClickListener( new MyOnCLickListener() );
答案 1 :(得分:1)
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);
}
}
}
替换您的代码&写上面的代码。
答案 2 :(得分:0)
将代码放在公共/受保护的函数中。由于onClickListener是你的一个抽象类,它可以从嵌套它的类中访问公共或受保护的方法。