java - 如何从首选项中的 AlertDialog 获取值

标签 java android android-alertdialog android-preferences preferences

我有设置类。我在里面使用偏好。在首选项中,我使用 AlertDialog,其中包含 EditTextradioButton。 我想从我的设置 Activity 中获取编辑文本和单选按钮的值,但我在网上看到的所有内容都只是关于偏好或 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();
      }

我的 Preference.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>

最佳答案

MySettingsFragmentOnPreferenceTreeClick 函数中:

@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();

                View dialogView = inflater.inflate(R.layout.set_time, null) // add this line

                builder.setView(dialogView).
                        setPositiveButton(R.string.save_settings, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                   // take the value from selected radio button 
                                   RadioGroup radioGroup = (RadioGroup) dialogView.findViewById(R.id.radio_group);
                                   int selectedId = radioGroup.getCheckedRadioButtonId();

                                    // find the radiobutton by returned id
                                    RadioButton radioButton = (RadioButton) findViewById(selectedId);

                                   // get edittext value
                                   EditText edittext = (EditText) dialogView.findViewById(R.id.num_input);
                                   String edittextValue = edittext.getText().toString();


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

关于java - 如何从首选项中的 AlertDialog 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59399503/

相关文章:

java - 发送多个 "println"给客户端

android - 如何在 Firebase 中将用户属性设置为 "Purchasers"?

android - Android ListView 中的奇怪异常

android - 在 alertDialog 中使用微调器时出现空指针异常

java - 如何检查哪些复选框已选中,哪些未选中?

java - FTPClient listFiles 方法返回目录。我该如何过滤?

java - 如何从嵌套数组中提取 JSON 数据?使用安卓

java - 我如何最好地从 SnakeYaml 加载非 bean 对象

java - android中的路径交叉点

android - 创建 AlertDialog 时会触发什么方法?