简体   繁体   中英

Android - Preference OnPreferenceClick is never called

I really need some help here! Logcat doesn't give out any errors, no errors in code and still nothing is happening. I got 2 normal Preferences "ueber", which works anytime I click it and "update", which is never called. I know that because i copied the exact code of "ueber" to "update" and still nothing was happening... I'm already sitting 2 weaks on that and you guys are my last hope!

preferencesettings2.xml

<PreferenceScreen>
<PreferenceCategory android:title="Info">
    <Preference
        android:key="update"
        android:title="Update"
        android:summary="Check for updates"/>
    <Preference 
        android:key="about"
        android:title="About"
        android:summary="Shows information"/>
</PreferenceCategory>
</PreferenceScreen>

Preferencesettings2.java

public class Preferencesettings2 extends PreferenceActivity implements OnPreferenceClickListener { @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferencesettings2); @Override public boolean onPreferenceClick(Preference preference) { if(preference.getKey().equals("update")){ download(); }else if(preference.getKey().equals("about")){ AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Preferencesettings2.this); alertDialogBuilder.setTitle("about"); alertDialogBuilder.setMessage("About"); alertDialogBuilder.setCancelable(true); alertDialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { dialog.cancel(); } }); alertDialogBuilder.show(); } return true; }

Try locating the preference with findPreference and calling setOnPreferenceClickListener :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferencesettings2);
    findPreference("about").setOnPreferenceClickListener(this);
    findPreference("update").setOnPreferenceClickListener(this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM