日期选择器对话框不起作用更改一个文本更改另一个文本

Date Picker Dialog Not Working Changing In One Text Make Change To Another

我想使用 DatePicker 对话框在 textview 中获取日期。我在 Activity 打开时在 EditText 中获取了当前日期。我已经在 EditTexts 上实现了 DatePicker 对话框,但是当我尝试通过单击“编辑文本”来更改日期时,它会将更改日期更改为表单中的另一个文本视图...

基本上我有 2 个 EditText,我在两者上都实现了 DatePicker 对话框..

这是我的 XML 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:orientation="vertical">


<TextView
    android:layout_marginTop="40dp"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/txtclientname"
    />

<EditText
    android:layout_marginTop="80dp"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:inputType="date"
    android:id="@+id/currdate"
    android:textColor="@color/colorText"
    />

<EditText
    android:layout_marginTop="80dp"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/currdate1"
    android:layout_toRightOf="@+id/currdate"
    android:textColor="@color/colorText"
    android:inputType="date"
    />

这是我的 Jave 文件

{

TextView EdtName;
EditText EditCurrDate,EdtCurrDate2;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view =  inflater.inflate(R.layout.invoice, container, false);

    String date_n = new SimpleDateFormat("MMM / dd / yyyy", Locale.getDefault()).format(new Date());

    EditCurrDate=view.findViewById(R.id.currdate);
    EdtCurrDate2=view.findViewById(R.id.currdate1);
    EdtName=view.findViewById(R.id.txtclientname);

    EditCurrDate.setText(date_n);

    EdtCurrDate2.setText(date_n);

    EditCurrDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar mcurrentDate=Calendar.getInstance();
            int year = mcurrentDate.get(Calendar.YEAR);
            int month = mcurrentDate.get(Calendar.MONTH);
            int day = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
                    // TODO Auto-generated method stub

                    Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
                    EditCurrDate.setText((selectedMonth+1) + "/" + selectedDay + "/" + selectedYear);
                }
            },year, month, day);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();
        }
    });

    EdtCurrDate2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar mcurrentDate=Calendar.getInstance();
            int year1 = mcurrentDate.get(Calendar.YEAR);
            int month1 = mcurrentDate.get(Calendar.MONTH);
            int day1 = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
                    // TODO Auto-generated method stub

                    Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
                    EditCurrDate.setText((selectedMonth+1) + "/" + selectedDay + "/" + selectedYear);
                }
            },year1, month1, day1);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();
        }
    });


    return view;
}

}

我只是在两个点击侦听器中使用相同的编辑文本

 {

TextView EdtName;
EditText EditCurrDate;
EditText EdtCurrDate2;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view =  inflater.inflate(R.layout.invoice, container, false);

    String date_n = new SimpleDateFormat("MMM / dd / yyyy", Locale.getDefault()).format(new Date());

    EditCurrDate=view.findViewById(R.id.currdate);
    EdtCurrDate2=view.findViewById(R.id.currdate1);
    EdtName=view.findViewById(R.id.txtclientname);

    EditCurrDate.setText(date_n);

    EdtCurrDate2.setText(date_n);

    EditCurrDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar mcurrentDate=Calendar.getInstance();
            int year = mcurrentDate.get(Calendar.YEAR);
            int month = mcurrentDate.get(Calendar.MONTH);
            int day = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
                    // TODO Auto-generated method stub

                    Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
                    EditCurrDate.setText((selectedMonth+1) + "/" + selectedDay + "/" + selectedYear);
                }
            },year, month, day);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();
        }
    });

    EdtCurrDate2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar mcurrentDate=Calendar.getInstance();
            int year1 = mcurrentDate.get(Calendar.YEAR);
            int month1 = mcurrentDate.get(Calendar.MONTH);
            int day1 = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
                    // TODO Auto-generated method stub

                    Log.e("Date Selected", "Month: " + selectedMonth + " Day: " + selectedDay + " Year: " + selectedYear);
                    EdtCurrDate2.setText((selectedMonth+1) + "/" + selectedDay + "/" + selectedYear);
                }
            },year1, month1, day1);
            mDatePicker.setTitle("Select date");
            mDatePicker.show();
        }
    });


    return view;
}

}

您基本上是在第一个和第二个对话侦听器中将文本设置为相同的编辑文本 EdtCurrDate

所以你必须将第二个更改为 EdtCurrDate2

Note : i suggest using date patterns in order to retrieve date strings from a date object

示例:

public class DateUtils {

//Date patterns examples 
public static String UI_DATE_PATTERN = "EEE, dd MMMM yyyy, HH:mm";
public static String SIMPLE_DATE_PATTERN = "dd/MM/yyyy"; // <-- Your desired date pattern

//converts date to a string
public static String toString(Date date, String pattern) {
    try {
        return new SimpleDateFormat(pattern, Locale.getDefault()).format(date);
    } catch (Exception e) {
        return e.getMessage();
    }
}

//Gets the date from a string
public static Date fromString(String dateString, String stringPattern) {
    try {
        return new SimpleDateFormat(stringPattern, Locale.getDefault()).parse(dateString);
    } catch (ParseException e) {
        return null;
    }
}
}

用法 :

 Date currentDate = Calendar.getInstance().getTime(); // <- Your date object

 String currentDateSimpleString = DateUtils.toString(currentDate, DateUtils.SIMPLE_DATE_PATTERN); 

输出:05/22/2019

 String currentDateUiString = DateUtils.toString(currentDate, DateUtils.UI_DATE_PATTERN);

输出:2019 年 5 月 5 日,星期三,17:09

 Date dateFromSimpleString = DateUtils.fromString(currentDateSimpleString , DateUtils.SIMPLE_DATE_PATTERN); 

输出等于 currentDate

 Date dateFromUiString = DateUtils.fromString(currentDateUiString, DateUtils.UI_DATE_PATTERN); 

输出等于 currentDate

Note : To retrieve a date from a string, the pattern must have its exact same format otherwise it will throw an exception.