Android setClickListner fuori OnCreate
-
29-10-2019 - |
Domanda
Questa è la mia funzione oncreate.
Dal momento che c'è molto codice all'interno di OnClickListener per ciao, voglio spostarlo al di fuori di un metodo. Come lo posso fare ?
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);
}
});
}
Soluzione
Crea una classe interna che si estende su OnClickListener.
private class MyOnClickListener implements OnClickListener { ... }
Qui aggiungi la stessa identica implementazione che tutti avete pronto per l'anonimo OnClickListener. Assicurati solo che tutte le variabili a cui accedi siano dichiarate variabili locali nella tua classe di attività.
Quindi quando imposti OnClickListener lo fai e basta:
hello.setOnClickListener( new MyOnCLickListener() );
Altri suggerimenti
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);
}
}
}
Sostituisci il codice e scrivi il codice sopra per quello.
Posizionare il codice in una funzione pubblica / protetta. Poiché OnClickListener è una classe astratta all'interno della tua, può accedere ai metodi pubblici o protetti della classe che la nidificano.