简体   繁体   中英

How get a value from AlertDialog in Preference

I have Settings class. And I use preference inside. In the preference I use AlertDialog which has EditText and radioButton in it. I want to get the values of the edit text and the radio button from my settings activity, but everything I see online is just about preference OR AlertDialog and I don't understand what to do if they are combined.

my 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();
      }

my 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>

my setTime.xml (the dialog):

<?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>

In your OnPreferenceTreeClick function in MySettingsFragment :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM