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

时间:2018-05-01 15:41:04

标签: android android-edittext alertdialog

单击从CalendarPickerFragment中选择日期的按钮后,如何更改 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); } 上文本的当前字符串。

这是AlertDialog

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

CalendarPickerFragment

int main()
{
    int tab[28][120];
    create(tab, 28, 120);
    paint(tab, 28, 120);
    char i = '1';
    char direction = 'R';
    while(i != 'q')
    {
        if (kbhit())
            i =getch();
        translate(tab, direction, 28, 120);
        paint(tab, 28, 120);
        Sleep(300);
    }   
}

1 个答案:

答案 0 :(得分:2)

好的,你可以使用你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");