简体   繁体   中英

Android : Dialog in Preferences.xml [Need Help]

In my preference Activity, i'm using an alert dialog, but i get error on OnSharedPreferenceChangeListener , am i doing wrong somewhere? Please help me out, Thanks!

 public class MainSettings extends     PreferenceActivity implements //getting error here     OnSharedPreferenceChangeListener
{


    private WebView webView4;


    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);          
        addPreferencesFromResource(R.xml.preferences);        


    }

          public boolean onPreferenceTreeClick
(PreferenceScreen preferenceScreen,
                    final Preference preference)
{
             if
(preference.equals("dialog_preference"))
{


         AlertDialog.Builder adb = new AlertDialog.Builder(this);
        adb.setCancelable(false); 
            adb.setView(webView4);
            adb.setTitle("What's New?");
            adb.setIcon(R.drawable.alert_icon);
            adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {


                    public void onClick(DialogInterface dialog, int id) {  


                    }  
                }); 


            adb.show();



      }

You need to declare it as:

public class MainSettings extends PreferenceActivity implements OnSharedPreferenceChangeListener {
  ...
}

And, in order for OnSharedPreferenceChangeListener to be compiled, you'll need to import the following class:

import android.content.SharedPreferences.OnSharedPreferenceChangeListener;

OnSharedPreferenceChangeListener is included in android.jar file, so make sure you included this library in your project.

Plus, you'll need to define the onSharedPreferenceChanged method to capture changes to shared preferences:

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
   if (key.equalsIgnoreCase("your_key")) {
      // display your dialog here
   }
}

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