当我在PreferenceScreen中选中或取消选中CheckBoxPreference时,如何启动事件?

时间:2014-03-10 01:54:50

标签: android

我希望在PreferenceScreen中选中或取消选中CheckBoxPreference时执行一个函数,我该怎么办?谢谢!

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="AppPreference"
    android:summary="@string/PreferenceSummary"
    android:title="@string/Preference" >

     <CheckBoxPreference
        android:defaultValue="true"
        android:key="AlwaysHideIcon"
        android:title="@string/AlwaysHideIconTitle"
        android:summary="@string/AlwaysHideIconSummary"  
        android:layout="@layout/sms_custom_preference_layout"  
     />   

</PreferenceScreen>

public class SmsPreference extends PreferenceActivity{
     @Override
     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);         
            addPreferencesFromResource(R.xml.smspreference);
            setContentView(R.layout.sms_custom_preference);

            // Look up the AdView as a resource and load a request.
            if (PublicPar.IsRegistered==false) {
                AdView adView = (AdView)this.findViewById(R.id.adView);
                adView.loadAd(new AdRequest());
            }


            Button btnClose=(Button)findViewById(R.id.btnClosePreference);
            btnClose.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    finish();
                }           
            });

     }

}

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"   
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/border_ui"
  android:orientation="vertical">        

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/adView"    
        android:layout_above="@+id/linearLayout1"
        >        
    </ListView> 

    <LinearLayout
        android:id="@+id/linearLayout1"  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="4" >

        <Button
            android:id="@+id/btnClosePreference"
            style="@style/myTextAppearance"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:text="@string/BtnReturn" >
        </Button>
    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

    CheckBoxPreference preference = (CheckBoxPreference) findPreference("preference");
    preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() 
    { 
        public boolean onPreferenceChange(Preference preference, Object newValue) 
        {
            Log.d("MyApp", "Pref " + preference.getKey() + " changed to " + newValue.toString());

            if(newValue.toString().equalsIgnoreCase("true"))
                //Here manage your event for checked preference
            else
                //Here manage your event for Unchecked preference

            return true;
        }
    });