使用PreferenceFragment时按钮不起作用

时间:2017-08-25 08:02:44

标签: android

当我点击按钮 btnClosePreference 时,我希望系统可以关闭用户界面。

但事实上,当我点击按钮 btnClosePreference 时,系统没有响应,代码Toast.makeText(getApplicationContext(), "My",Toast.LENGTH_LONG).show();也没有被解雇。

为什么?

LockPreference.java

public class LockPreference extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new MyPreferenceFragment())
                .commit();

    }

    public class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.unlockpreference);

            getActivity().setContentView(R.layout.unlock_custom_preference);

            SetDisplayIcon();

            Button btnClose = (Button) findViewById(R.id.btnClosePreference);

            btnClose.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "My",Toast.LENGTH_LONG).show();
                    getActivity().finish();
                }
            });
        }

        private void SetDisplayIcon() {
            final String iconKey = getString(R.string.IconKey);
            CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference(iconKey);

            checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                        public boolean onPreferenceChange(
                                Preference preference, Object newValue) {

                            SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext());
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putBoolean(iconKey, (Boolean) newValue);
                            editor.commit();

                            PublicParFun.DisplayIconBySettings(
                                    getApplicationContext(), iconKey);

                            return true;
                        }
                    });
        }

    }

}

unlockpreference.xml

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

    <ui.custom.DialogChangePassword
       android:key="ChangePassword"
       android:dialogIcon="@android:drawable/ic_dialog_alert"
       android:title="@string/ChangePasswordTitle"
       android:summary="@string/ChangePasswordSummary"      
       android:positiveButtonText="@string/BtnSave"
       android:negativeButtonText="@string/BtnCancel"
       android:layout="@layout/unlock_custom_preference_item"  
    />      

    <CheckBoxPreference
       android:defaultValue="true"
       android:key="DisplayNotificationIcon"
       android:title="@string/DisplayNotificationIconTitle"
       android:summary="@string/DisplayNotificationIconSummary"  
       android:layout="@layout/unlock_custom_preference_item" 
    />    

</PreferenceScreen>

unlock_custom_preference.xml

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

    <com.google.android.gms.ads.AdView  
        xmlns:ads="http://schemas.android.com/apk/res-auto"       
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        android:layout_alignParentTop="true"   
        ads:adUnitId="@string/ad_unit_id" />


    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        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/myTextMedium"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:text="@string/BtnReturn" >
        </Button>
    </LinearLayout>
</RelativeLayout>

0 个答案:

没有答案