繁体   English   中英

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

[英]How can I launch a event when I check or uncheck CheckBoxPreference in PreferenceScreen?

我希望在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>
    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;
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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