OnItemClickListener或ListPreference

时间:2018-03-21 16:59:56

标签: java android listpreference

首先,我是德国人,对不起,如果我的英语不太好。我会尽力描述我的问题。 我有一个ListPreference,用户应该在其中选择他们的类。当用户点击任何条目时,他们将加入Firebase中的主题。我的问题是“当用户点击任何条目时”。我不知道如何做到这一点,如果用户点击任何条目,任何事情都会发生。我已经尝试过了:

ListPreference pref_klassen_list_key = (ListPreference) findPreference("pref_klassen_list_key");

pref_klassen_list_key.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object o) {

                return true;
            }
        });

与onPreferenceClick相同。但是两种方法都在错误的时间调用:1。选择类2.关闭ListPreference 3.再次打开ListPreference - >调用onPreferenceChange并再次选择class - > onPreferenceClick被调用,但我试图用吐司显示接受的类,他们显示了在1中选择的类。我希望你理解我的问题。我希望有一个方法,当我这样做时调用1.以下是我的活动: 1. SettingsActivity.java

package main.faseflex.de.fgnapp;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;

public class SettingsActivity extends AppCompatPreferenceActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();



}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}


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


        ListPreference pref_klassen_list_key = (ListPreference) findPreference("pref_klassen_list_key");


        pref_klassen_list_key.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object o) {
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
                String prefList = sharedPreferences.getString("pref_klassen_list_key", "0");

                int integer = Integer.parseInt(prefList);


                if (integer == 0) {
                    Toast.makeText(getActivity(), "Klasse 5.1", Toast.LENGTH_LONG).show();
                } else if (integer == 1) {
                    Toast.makeText(getActivity(), "Klasse 5.2", Toast.LENGTH_LONG).show();
                } else if (integer == 2) {
                    Toast.makeText(getActivity(), "Klasse 5.3", Toast.LENGTH_LONG).show();
                } else if (integer == 3) {
                    Toast.makeText(getActivity(), "Klasse 6.1", Toast.LENGTH_LONG).show();
                } else if (integer == 4) {
                    Toast.makeText(getActivity(), "Klasse 6.2", Toast.LENGTH_LONG).show();
                } else if (integer == 5) {
                    Toast.makeText(getActivity(), "Klasse 7.1", Toast.LENGTH_LONG).show();
                } else if (integer == 6) {
                    Toast.makeText(getActivity(), "Klasse 7.2", Toast.LENGTH_LONG).show();
                } else if (integer == 7) {
                    Toast.makeText(getActivity(), "Klasse 8.1", Toast.LENGTH_LONG).show();
                } else if (integer == 8) {
                    Toast.makeText(getActivity(), "Klasse 8.2", Toast.LENGTH_LONG).show();
                } else if (integer == 9) {
                    Toast.makeText(getActivity(), "Klasse 9.1", Toast.LENGTH_LONG).show();
                } else if (integer == 10) {
                    Toast.makeText(getActivity(), "Klasse 9.2", Toast.LENGTH_LONG).show();
                } else if (integer == 11) {
                    Toast.makeText(getActivity(), "Klasse 10", Toast.LENGTH_LONG).show();
                } else if (integer == 12) {
                    Toast.makeText(getActivity(), "Klasse 11", Toast.LENGTH_LONG).show();
                }
                return true;
            }
        });

    }

}

这是preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <Preference
        android:title="Es sind noch nicht alle Funktionen komplett implementiert."
        android:selectable="false" />

    <PreferenceCategory android:title="Allgemeines">

        <Preference
            android:selectable="false"
            android:summary="Hier werden später allgemeine Einstellungen stehen." />

    </PreferenceCategory>

    <PreferenceCategory android:title="Benachrichtigungen">

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="pref_about_checkbox_key"
            android:title="Benachrichtigungen erhalten"
            android:summary="Benachrichtigung bei Änderung im Stundenplan"/>

        <ListPreference
            android:title="Klasse auswählen"
            android:summary="%s"
            android:key="pref_klassen_list_key"
            android:dependency="pref_about_checkbox_key"
            android:defaultValue="1"
            android:entries="@array/pref_klassen_entries"
            android:entryValues="@array/pref_klassen_values" />

    </PreferenceCategory>

    <PreferenceCategory android:title="Über die App">

        <Preference
            android:summary="Danke für dein Feedback!"
            android:title="Feedback"
            android:key="pref_about_feedback_key" />

        <Preference
            android:summary="@string/versionshort"
            android:title="Version"
            android:key="@string/pref_about_version_key"/>

        <Preference
            android:summary=""
            android:title="Impressum"
            android:key="pref_about_impressum_key"/>

    </PreferenceCategory>

    <PreferenceCategory android:title="Entwickler-Einstellungen">

        <Preference
            android:title="Daten zu Benachrichtigungen"
            android:summary="technische Informationen zu Push-Benachrichtigungen"
            android:key="pref_fcmtoken_key" />

    </PreferenceCategory>

</PreferenceScreen>

我正在从微调器中搜索OnItemSelectedListener。假如是否需要任何其他文件或您不明白任何内容。我期待着您的回复。

0 个答案:

没有答案