如何从“偏好设置”的AlertDialog中获取值

问题描述 投票:1回答:1

我有设置类。我在里面使用首选项。在首选项中,我使用其中包含AlertDialogEditTextradioButton。我想从我的设置活动中获取编辑文本和单选按钮的值,但是我在网上看到的所有内容仅与首选项OR AlertDialog有关,并且如果将它们组合在一起,我不知道该怎么办。

我的Settings.java:

public class Settings extends AppCompatActivity {
    private RadioGroup radioGroup;
    AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_setting, new MySettingsFragment())
                .commit();

    }

    public static class MySettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
       setPreferencesFromResource(R.xml.preference, rootKey);
    }

    @Override
    public boolean onPreferenceTreeClick(Preference p){
       final EditText input = new EditText(this.getContext());
       if(p.getKey().equals("appointment type")){
          return true;
       }

       if(p.getKey().equals("time frame")){
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          LayoutInflater inflater = requireActivity().getLayoutInflater();
          builder.setView(inflater.inflate(R.layout.set_time, null)).
                        setPositiveButton(R.string.save_settings, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               **Here i dont know how to take the value**


                            }
                        })
                        .setNegativeButton(R.string.cancel_settings, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                AlertDialog dialog = builder.create();
                dialog.show();
      }

我的Prefernce.xml:

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

    <EditTextPreference
        android:key="appointment type"
        android:title="Set appointment types"
        android:icon="@drawable/appoitment_type"
        />

    <Preference
        android:id="@+id/timeFrame"
        android:key="time frame"
        android:title="Allow change meeting"
        android:summary="Time frame where clients can't change a meeting"
        android:onClick = "popUpWindow"
        android:icon="@drawable/no_meetings_changes"
        />

    <Preference
        android:id="@+id/remindTime"
        android:key="remind time"
        android:title="Meetings reminder"
        android:summary="How much time to remind client before a meeting"
        android:icon="@drawable/send_reminder" />

</PreferenceScreen>

我的setTime.xml(对话框):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryLight">

    <EditText
        android:id="@+id/num_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/default_num"
        android:inputType="text" />

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/minutes_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minutes" />

        <RadioButton
            android:id="@+id/hours_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text = "@string/hours"/>

        <RadioButton
            android:id="@+id/days_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text = "@string/days"/>

        <RadioButton
            android:id="@+id/weeks_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text = "@string/weeks"/>

    </RadioGroup>
</LinearLayout>
java android android-alertdialog android-preferences preferences
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.