How to add a button to a PreferenceScreen?

后端 未结 10 1304
抹茶落季
抹茶落季 2020-11-28 20:48

I\'m quite new to Android Development and just came across Preferences. I found PreferenceScreen and wanted to create a login functionality with it. The only pr

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 21:27

    I wanted to add an Exit link to the preferences and was able to modify Jakar's code to make it work like this:

       
       
                  
           
    
    

    Originally the 'Preference' was a 'EditTextPreference' which I hand edited.

    Then in the class:

    public class MyPreferences extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.mypreferences);
    
        Preference button = (Preference)getPreferenceManager().findPreference("exitlink");      
        if (button != null) {
            button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference arg0) {
                    finish();   
                    return true;
                }
            });     
        }
    }
    }
    

提交回复
热议问题