【问题标题】:How do I remove concatenate error in Android Studio on an EditText?如何在 EditText 上删除 Android Studio 中的串联错误?
【发布时间】:2016-01-21 03:50:21
【问题描述】:

我有一个 DatePickerFragment,它可以让用户从日历中选择一个日期。然后将捕获的日期设置在 EditText 行上。 onDateSet 中的以下代码在以前版本的操作系统上运行良好,现在它给出了以下错误并且日期未按预期显示:mm/dd/yyyy。该错误在“txtDate.setText...”行上突出显示。我在这里做错了什么?

public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);

    // If the argsbundle from the Activity has data (because the user previously selected a date) then
    // set that date in the DatePicker.
    if (getArguments() != null) {
        c = Calendar.getInstance();
        year = getArguments().getInt("year");
        month = getArguments().getInt("month");
        day = getArguments().getInt("day");
        c.set(year, month, day);
    } else { // If the DueDate EditText line is empty (no previously selected date by the user then
        // set today's date into the DatePicker.
        // Calendar class obtains the current date on the device and has fields for
        // each of the parts of the date: day, month and year.
        c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
    }

    DatePickerDialog picker = new DatePickerDialog(getActivity(),
            this, year, month, day);
    // Set monthly calendar rather than default spinner.
    picker.getDatePicker().setCalendarViewShown(true);
    // Turn off date selector spinner.
    picker.getDatePicker().setSpinnersShown(false);
    picker.setTitle("Select a Due Date");

    return picker;
}

部分 DatePickerFragment.java 文件:

...
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
     // Set the selected date into the FEditText line.
     EditText txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
     // Format the selected date.
     txtDate.setText((monthOfYear + 1) + "/" + dayOfMonth + "/" + year + " ");
     // Display the date.  "Month" uses a zero-based index from 0 to 11, so need to add 1 to show properly.
     txtDate.setSelection(txtDate.getText().length());
}

【问题讨论】:

  • 这是一个警告,很清楚它在说什么。 1)字符串与数字的连接可能不会产生预期的结果,并且 2)您应该通过 res/strings.xml 使用字符串资源
  • 我是编程和 Android 新手……我不清楚,这就是我问这个问题的原因。

标签: android android-edittext android-datepicker


【解决方案1】:

你可以这样做:

 String date = String.format("%d/%d/%d ",monthOfYear + 1, dayOfMonth, year);
 txtDate.setText(date);

或者:

int day = view.getDayOfMonth();
int month = view.getMonth();
int year =  view.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, day);

Date asDate = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy", Locale.US);
txtDate.setText(sdf.format(asDate));

【讨论】:

  • 好的,我试试。我是安德的新手。编程,什么是 %d?
  • String.format() 是一个辅助方法,它将用字符串参数之后的参数替换 %d%s 字符。见this link
  • 好的,很酷。当我运行字符串日期代码时,我最终只显示年份,没有日期或月份(例如,2015 年为 15)。当我用“year-2000”替换“year”时,我得到 10/21/15 但我正在寻找 10/21/2015。
  • 好的。我读过一堆相互冲突的在线帖子,说 SimpleDateFormat 可能会导致问题,因为它可能不是线程安全的。你对此有什么想法吗?
  • 如果您在onDateSet 处理程序中使用简单的日期格式,那么您不必担心线程安全。我会改用joda date,它确实有一个线程安全的日期时间格式化程序(并且代码更干净)。
猜你喜欢
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
  • 2015-03-31
  • 1970-01-01
相关资源
最近更新 更多