Android setclicklistner en dehors de l'onCreate
-
29-10-2019 - |
Question
Ceci est ma fonction onCreate.
Puisqu'il y a beaucoup de code à l'intérieur d'ONClickListener pour Hello, je veux le déplacer à l'extérieur vers une méthode. Comment puis je faire ça ?
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);
}
});
}
La solution
Créez une classe intérieure étendant OnClickListener.
private class MyOnClickListener implements OnClickListener { ... }
Ici, vous ajoutez la même implémentation exacte que vous avez tous prêts pour l'Anonymous OnClickListener. Assurez-vous simplement que toutes les variables auxquelles vous accédez sont déclarées comme variables locales dans votre classe d'activité.
Ensuite, lorsque vous définissez le OnClickListener, vous faites simplement ceci:
hello.setOnClickListener( new MyOnCLickListener() );
Autres conseils
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);
}
}
}
Remplacez votre code et écrivez le code ci-dessus pour cela.
Placez le code dans une fonction publique / protégé. Étant donné qu'OnclickListener est une classe abstraite à l'intérieur du vôtre, il peut accéder aux méthodes publiques ou protégées de la classe qui la niche.