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

时间:2019-05-22 09:13:13

标签: android android-edittext android-datepicker

我想使用DatePicker对话框在textview中设置日期。我在“活动打开”时在EditText中获得了“当前日期”。我已经在EditTexts上实现了DatePicker对话框,但是当我尝试通过单击Edit Text来更改日期时,它将更改日期更改为另一文本视图的形式...

基本上我有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;
}

}

2 个答案:

答案 0 :(得分:1)

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

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

  

注意:我建议使用日期模式以便从日期对象中检索日期字符串

示例:

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

输出: 2019/05/22

 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

  

注意:要从字符串中检索日期,该模式必须具有完全相同的格式,否则将引发异常。

答案 1 :(得分:0)

我只是在两个点击监听器中使用了相同的edittext

 {

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

}