gpt4 book ai didi

android - 如何更改正在运行的 AlertDialog 的文本

转载 作者:行者123 更新时间:2023-11-29 18:54:53 25 4
gpt4 key购买 nike

单击从 CalendarPickerFragment 中选择日期的按钮后,如何更改 AlertDialog 上文本的当前字符串。

这是 AlertDialog

    public class FragmentInicio extends Fragment{
private static String date;

public void addDate(){

View view = (LayoutInflater.from(getActivity())).inflate(R.layout.alert_dialog,null);

AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setView(view);

final TextView date_Text = view.findViewById(R.id.dateEditText);
final ImageButton dateButton= view.findViewById(R.id.tareaFechaButton);
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CalendarPickerFragment newFragment = new CalendarPickerFragment();
newFragment.show(getFragmentManager(), "DatePicker");

}
});

date_Text.setText(date);

alertDialog.setTitle("New Date");
alertDialog.setCancelable(true).setPositiveButton("Save", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Some stuff
}
});

alertDialog.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Dialog dialog = alertDialog.create();
dialog.show();

}

public static void displaydate(int year, int monthOfYear, int dayOfMonth) {
date = String.format("%02d", dayOfMonth) + "/" + String.format("%02d", monthOfYear + 1) + "/" +
String.format("%02d", year);
Log.d("DTAG", "date: "+date);
}

日历选择器 fragment

public class CalendarPickerFragment extends DialogFragment implements  DatePickerDialog.OnDateSetListener {

private CalendarPickerFragment.OnDateReceiveCallBack mListener;
private Context context;

public interface OnDateReceiveCallBack {
public void onDateReceive(int dd ,int mm, int yy);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;

try {
mListener = (CalendarPickerFragment.OnDateReceiveCallBack) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnDateSetListener");
}
}

public CalendarPickerFragment(){
}



@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

final Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Log.d(TAG, i + "/" + "/" + i1 + "/" + i2);
mListener.onDateReceive(i,i1,i2);
}

最佳答案

好的,您可以使用已经在 alertDialog 中创建的监听器以下应该是您的 CalendarPickerFragment

    public class CalendarPickerFragment extends DialogFragment implements  DatePickerDialog.OnDateSetListener {

private CalendarPickerFragment.OnDateReceiveCallBack mListener;
private Context context;

private static CalendarPickerFragment.OnDateReceiveCallBack mListenerForAlertDialog;

public interface OnDateReceiveCallBack {
public void onDateReceive(int dd, int mm, int yy);
}

public static CalendarPickerFragment getInstance(CalendarPickerFragment.OnDateReceiveCallBack callback) {
mListenerForAlertDialog = callback;
return new CalendarPickerFragment();
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;

try {
mListener = (CalendarPickerFragment.OnDateReceiveCallBack) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnDateSetListener");
}
}

public CalendarPickerFragment() {
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

final Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Log.d("tag", i + "/" + "/" + i1 + "/" + i2);
mListener.onDateReceive(i, i1, i2);
mListenerForAlertDialog.onDateReceive(i, i1, i2);
}

}

现在初始化这个fragmnet如下

CalendarPickerFragment newFragment =  CalendarPickerFragment.getInstance(new CalendarPickerFragment.OnDateReceiveCallBack() {
@Override
public void onDateReceive(int dd, int mm, int yy) {
// boom you wil get the call back on each date selection here
//eidtTextOfALertDialog.setText("whatEver")
}
});
newFragment.show(getFragmentManager(), "DatePicker");

关于android - 如何更改正在运行的 AlertDialog 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50119457/

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