Button in preference file

2020-05-06 10:06发布

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条回答
爷、活的狠高调
2楼-- · 2020-05-06 10:41

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() { });
}
}
查看更多
登录 后发表回答