Android PreferenceFragment无法正常运行

时间:2015-12-15 09:06:53

标签: android android-preferences preferencefragment

我在Android中开发应用。我有一个AppCompatActivity,我想在我的活动布局中添加一些首选项开关(总共3个)。我包含一个PreferenceFragment类来执行它,外观就可以了(这就是我想要的)。

活动外观: Activity appearance 但是当我打开任何一个开关时,所有开关都会自动开启和关闭。我知道因为我有一个OnChangePreferenceListener来在每个开关上做出不同的事情。 为什么会这样?我做错了什么? 这是我的preference.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreference
        android:key="startButton"
        android:title="Start/Stop run"
        android:defaultValue="false" />
    <SwitchPreference
        android:key="pauseButton"
        android:title="Pause run"
        android:defaultValue="false" />
    <SwitchPreference
        android:key="soundButton"
        android:title="Sound on/off"
        android:defaultValue="false" />
</PreferenceScreen>

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <TextView
        android:id="@+id/textViewNombreSimul"
        android:layout_width="136dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start|center_vertical"
        android:layout_marginTop="20dp"
        android:text="@string/txt_simulname"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14sp"
        android:labelFor="@+id/editTextNombreSimul" />

    <EditText
        android:id="@+id/editTextNombreSimul"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|start"
        android:enabled="false"
        android:inputType="text"
        android:text="" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textAtsu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|center_vertical"
                android:layout_marginTop="20dp"
                android:text="@string/txt_atsu"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="14sp"
                android:labelFor="@+id/editAtsu" />

            <EditText
                android:id="@+id/editAtsu"
                android:layout_width="112dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|start"
                android:enabled="false" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textAtcRole"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|center_vertical"
                android:layout_marginTop="20dp"
                android:text="@string/txt_atcrole"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="14sp"
                android:labelFor="@+id/editAtcRole" />

            <EditText
                android:id="@+id/editAtcRole"
                android:layout_width="136dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|start"
                android:enabled="false"
                android:inputType="text" />
        </LinearLayout>

    </LinearLayout>

    <TextView
        android:id="@+id/textAtcName"
        android:layout_width="136dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start|center_vertical"
        android:layout_marginTop="20dp"
        android:text="@string/txt_atcname"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14sp"
        android:labelFor="@+id/editAtcName" />

    <EditText
        android:id="@+id/editAtcName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|start"
        android:enabled="false"
        android:inputType="text" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textTimeoOff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|center_vertical"
                android:layout_marginTop="20dp"
                android:text="@string/txt_timeoff"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="14sp"
                android:labelFor="@+id/editTimeOff" />

            <EditText
                android:id="@+id/editTimeOff"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|start"
                android:enabled="false"
                android:inputType="numberDecimal" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingEnd="16dp"
            android:paddingStart="16dp">

            <TextView
                android:id="@+id/textTimeoOffInterval"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|center_vertical"
                android:layout_marginTop="20dp"
                android:text="@string/txt_timeinterval"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="14sp"
                android:labelFor="@+id/editTimeOffInterval" />

            <EditText
                android:id="@+id/editTimeOffInterval"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|start"
                android:enabled="false"
                android:inputType="number" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textTimeoOn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|center_vertical"
                android:layout_marginTop="20dp"
                android:text="@string/txt_timeon"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="14sp"
                android:labelFor="@+id/editTimeOn" />

            <EditText
                android:id="@+id/editTimeOn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|start"
                android:enabled="false"
                android:inputType="number" />
        </LinearLayout>

    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>
</RelativeLayout>

这是我的AppCompatActivity(没有不重要的方法):

public class RunConfigActivity extends AppCompatActivity {

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

        loggedUser = getIntent().getParcelableExtra("user");
        simul = getIntent().getParcelableExtra("simul");
        configBean = getIntent().getParcelableExtra("config");
        if(getIntent().hasExtra("output")){
            outputBean = getIntent().getParcelableExtra("output");
        }

        context = this;

        setContentView(R.layout.runconfig_activity);
        getFragmentManager().beginTransaction().replace(R.id.fragment, new PrefsFragment()).commit();
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tiempoOff = (EditText) findViewById(R.id.editTimeOff);
        intervalOff = (EditText) findViewById(R.id.editTimeOffInterval);
        tiempoOn = (EditText) findViewById(R.id.editTimeOn);
        nombreSimul = (EditText) findViewById(R.id.editTextNombreSimul);
        atsu = (EditText) findViewById(R.id.editAtsu);
        atcRole = (EditText) findViewById(R.id.editAtcRole);
        atcName = (EditText) findViewById(R.id.editAtcName);

        cargarDatosRun();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        if(!nombreSimul.isEnabled())
            getMenuInflater().inflate(R.menu.edrun_menu, menu);
        else
            getMenuInflater().inflate(R.menu.edition_activity,menu);
        activityMenu = menu;
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.runEditionMenu:
                activarEdicion();
                onCreateOptionsMenu(activityMenu);
                break;
            case R.id.ed_btnOK:
                if (comprobacionPrevia()) {
                    try {
                        borrarSimul(simul);
                        modificarSimulacion();
                        guardarSimul(simul);
                        modificarConfig();
                        borrarConfig(configBean);
                        guardarConfig(configBean);
                    } catch (ParserConfigurationException e) {
                        Log.e("ERROR", e.getMessage());
                    } catch (IOException e) {
                        Log.e("ERROR", e.getMessage());
                    } catch (SAXException e) {
                        Log.e("ERROR", e.getMessage());
                    } catch (TransformerException e) {
                        Log.e("ERROR", e.getMessage());
                    }
                    desactivarEdicion();
                    onCreateOptionsMenu(activityMenu);
                }
                break;
            case R.id.ed_btnReject:
                desactivarEdicion();
                onCreateOptionsMenu(activityMenu);
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    private void activarPref(String key){
        switch (key){
            case "startButton":
                startTime = Calendar.getInstance().getTimeInMillis();
                simul.setSimulName(nombreSimul.getText().toString());
                simul.setAtsu(atsu.getText().toString());
                simul.setAtcRole(atcRole.getText().toString());
                simul.setAtcName(atcName.getText().toString());
                Bundle bundle = new Bundle();
                bundle.putLong("startTime", startTime);
                bundle.putParcelable("simul", simul);
                bundle.putParcelable("user", loggedUser);
                bundle.putParcelable("config", configBean);
                Intent intent = new Intent(context, RunActivity.class);
                intent.putExtras(bundle);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
                break;
            case "pauseButton":
                finish();
                break;
            case "soundButton":
                break;
        }
    }

    private void desactivarPref(String key){
        switch (key){
            case "startButton":
                if(outputBean!=null) {
                    Calendar calendar = Calendar.getInstance();
                    outputBean.setFinPrueba(new SimpleDateFormat("hh:mm:ss").format(calendar.getTime()));
                    try {
                        generarFicheroCSV();
                    } catch (IOException e) {
                        Log.e("ERROR", e.getMessage());
                    }
                }
                break;
            case "pauseButton":
                finish();
                break;
            case "soundButton":
                break;
        }
    }


    public class PrefsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.preferences);


        }

        @Override
        public void onResume() {
            super.onResume();
            getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

        }

        @Override
        public void onPause() {
            getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
            super.onPause();
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            boolean b = sharedPreferences.getBoolean(key, false);
            if(b){
                activarPref(key);
            }else{
                desactivarPref(key);
            }
        }
    }

}

提前感谢您的回答。对不起我的英语。

0 个答案:

没有答案