2013-10-02 42 views
1

大家好我是android開發的新手。我想在用戶在首選activity.i列表首選項中選擇任何主題時打開一個警告對話框。我在谷歌搜索很多,但沒有找到任何適當的答案。我的PrefenceActivity。在列表首選項中單擊項目時顯示警告對話框-ios

public class Setting extends PreferenceActivity { 

    /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      Setting.setAppTheme(this); 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(R.xml.prefs); 

     } 


    String ListPreference; 

     public static void setAppTheme(Activity a) { 
      // Get the xml/preferences.xml preferences 
      SharedPreferences prefs = PreferenceManager 
          .getDefaultSharedPreferences(a); 
     int ListPreference = Integer.parseInt(prefs.getString("listPref", "3")); 
     if(ListPreference == 0) { 
       a.setTheme(R.style.AppBaseThemeDark); 
       return; 
       } else if(ListPreference == 1){ 
        a.setTheme(R.style.AppBaseThemeLight); 
        //Toast.makeText(getApplicationContext(),"TTS Engines not found.\n Install TTS Engins",Toast.LENGTH_LONG).show(); 
       } else if(ListPreference == 2){ 
        a.setTheme(R.style.AppBaseTheme); 
       } 

    } 

     public boolean onCreateOptionsMenu(Menu menu) { 
      super.onCreateOptionsMenu(menu); 
      getActionBar().setDisplayHomeAsUpEnabled(true); 

      return true; 
     } 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
       case android.R.id.home: 
         // app icon in action bar clicked; go home 
        getFragmentManager().popBackStack(); 
        finish(); 
         return true; 

      } 
      return super.onOptionsItemSelected(item); 


} 
} 

回答

1

我幾天前面臨同樣的問題,我實現了一個自定義首選項類,擴展ListPreference來做到這一點。這是我實現類:

public class LogCleanPreference extends ListPreference { 
    private int mClickedDialogEntryIndex; 

    private Context mContext; 

    public LogCleanPreference(Context ctxt) { 
     this(ctxt, null); 
    } 

    public LogCleanPreference(Context ctxt, AttributeSet attrs) { 
     super(ctxt, attrs); 

     mContext = ctxt; 

     setNegativeButtonText(ctxt.getString(R.string.alert_cancel)); 
    } 

    @Override 
    protected void onPrepareDialogBuilder(Builder builder) { 
     if (getEntries() == null || getEntries() == null) { 
      throw new IllegalStateException(
        "ListPreference requires an entries array and an entryValues array."); 
     } 

     mClickedDialogEntryIndex = findIndexOfValue(getValue()); 
     builder.setSingleChoiceItems(getEntries(), mClickedDialogEntryIndex, 
       new DialogInterface.OnClickListener() { 
      public void onClick(final DialogInterface dialog, final int which) { 
       // In my case I only show the AlertDialog if the user didn't select option number 2 
       if(which != 2){ 
        // Show AlertDialog 
       } 
       else{ 
        // Save preference and close dialog 
        mClickedDialogEntryIndex = which; 

        LogCleanPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); 
        dialog.dismiss(); 
       } 
      } 
     }); 

     builder.setPositiveButton(null, null); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 

     CharSequence[] mEntryValues = getEntryValues(); 

     if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) { 
      String value = mEntryValues[mClickedDialogEntryIndex].toString(); 
      if (callChangeListener(value)) { 
       setValue(value); 
      } 
     } 
    } 
} 

這是我用我的prefs.xml偏好:

<com.timeondriver.tod.settings.LogCleanPreference 
     android:defaultValue="0" 
     android:dialogTitle="@string/dialog_title_log_clean" 
     android:entries="@array/log_clean" 
     android:entryValues="@array/log_clean_values" 
     android:key="log_clean_preference" 
     android:summary="@string/summary_log_clean_preference" 
     android:title="@string/title_log_clean_preference" /> 
+0

出色的作品很棒 – user2837584

+0

很高興能幫到:) –

+0

當我說這個課程是嵌套在我原來的課程中的時候,我似乎無法正常工作。有小費嗎? –

0

1,創建一個變化監聽器:

private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object value) { 
      String stringValue = value.toString(); 

      if (preference instanceof ListPreference) { 
       // For list preferences, look up the correct display value in 
       // the preference's 'entries' list. 
       ListPreference listPreference = (ListPreference) preference; 
       int index = listPreference.findIndexOfValue(stringValue); 

       // Set the summary to reflect the new value. 
       preference 
         .setSummary(index >= 0 ? listPreference.getEntries()[index] 
           : null); 

      } 
      return true; 
     } 
    }; 

2,將偏好設置爲其值:

// Set the listener to watch for value changes. 
     preference 
       .setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); 

     // Trigger the listener immediately with the preference's 
     // current value. 
     sBindPreferenceSummaryToValueListener.onPreferenceChange(
       preference, 
       PreferenceManager.getDefaultSharedPreferences(
         preference.getContext()).getString(preference.getKey(), 
         "")); 

3rd,在第一步中,您可以使用if條件插入代碼來啓動對話框。

相關問題