我有设置类。我在里面使用偏好。在首选项中,我使用 AlertDialog
,其中包含 EditText
和 radioButton
。
我想从我的设置 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>
最佳答案
在 MySettingsFragment 的 OnPreferenceTreeClick
函数中:
@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/