gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:37:15 27 4
gpt4 key购买 nike

我有设置类。我在里面使用偏好。在首选项中,我使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com