Button in preference file

♀尐吖头ヾ 提交于 2019-12-20 07:17:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!