问题
I have set the two buttons in preference.xml file. I want to go to main activity when I click the "ok" button. So for that code is where to write?
回答1:
You can set an onPreferenceClickListener in your PreferenceActivity.
in preference xml :
<Preference android:title="Preference Button" android:summary="This works almost like a button" android:key="mypref" />
in preferences activity :
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Get the custom preference
Preference mypref = (Preference) findPreference("mypref");
mypref.setOnPreferenceClickListener(new OnPreferenceClickListener() { });
}
}
来源:https://stackoverflow.com/questions/9839679/button-in-preference-file