在RTL设备上更改日期格式?

问题描述 投票:4回答:7

我有一个在LTR设备上显示的日期字段(android 7.1.1) 问题是在RTL设备上镜像了日期字段内容

因此,在美国设备上,日期将正常显示:03/14/2019 在RTL Android设备上,日期将如下所示:2019/14/03

TextView的代码保存值:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/inspect_date"
            android:textSize="18sp" />

        <com.xerox.printerinspection.controls.DateEditText
            android:id="@+id/edit_inspect_date"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/lightGray"
            android:paddingStart="4dp"
            android:paddingEnd="4dp"
            android:drawableEnd="@android:drawable/ic_menu_my_calendar" />
    </LinearLayout>

我这样设定日期:

   Date currentDate = Calendar.getInstance().getTime();
   inspectionDateEdit.setDate(currentDate);

解决此问题的正确方法是什么?

UPDATE

父fragment_detail.xml标记如下所示:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lightGray"
    android:textDirection="locale"
    tools:context="com.xerox.MainActivity">
android right-to-left
7个回答
5
投票

由于父级的android:textDirection="locale"属性(甚至可能是默认值,当在android:supportsRtl="true"中设置Manifest.xml时 - 因此可以省略),必须为嵌套子节点定义文本方向 - 因此它不会继承父母的价值;这至少适用于EditText,当在RTL设备上强制LTR布局时:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/edit_inspect_date"
    android:text="@string/edit_inspect_date"
    android:textDirection="ltr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

screenshot

可能android:drawableEnd应该是android:drawableRight,因此它不会受到影响(这取决于,如果打算将它翻转到另一侧,无论是否)。


2
投票

首先,确定布局方向是否从右到左。

boolean isRightToLeft =
  TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) ==
  ViewCompat.LAYOUT_DIRECTION_RTL;

如果是,那么只需自己反转日期格式,

SimpleDateFormat format;
Date currentDate = Calendar.getInstance().getTime();
if (isRightToLeft) {
  // yup, use the mirrored date format
  format = new SimpleDateFormat("yyyy/dd/MM");
} else {
  // use the regular date format
  format = new SimpleDateFormat("MM/dd/yyyy");
}
currentDate.setTime(simpleDateFormat.parse(simpleDateFormat.format(currentDate)));
inspectionDateEdit.setDate(currentDate);

在运行时,操作系统将镜像你的yyyy/dd/MMMM/dd/yyyy


1
投票

您可以使用此表单

SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

1
投票

您只需将日期格式化为您想要的格式,不要担心,如下所示:

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String dateFormat = format.format(YourDate);

0
投票

您可以尝试将布局方向定义为xml文件中DateEditText的ltr


0
投票

我建议尝试change the parent android:textDirectionanyRTL

anyRtl - 段落方向是RTL,如果它包含任何强RTL字符,否则它是LTR,如果它包含任何强LTR字符。如果两者都没有,则段落方向是视图的已解析布局方向。”

原因是如果您将文本方向直接设置为特定的一个LTR或RTL或按设备区域设置(如您所做),则在尝试在RTL页面中间添加LTR文本时可能会遇到相同的问题,反之亦然。即在完整RTL页面中间的英语注释。一个更好的方法IMO是使用anyRTL,它将根据内容决定什么是正确的方向。

对于您的具体情况,拉丁数字中的日期始终为LTR。


0
投票

只需尝试下面的本地化代码。我尝试了两种本地化,它工作正常。

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
Date currentDate = Calendar.getInstance().getTime();
String currnetDateString = df.format(currentDate);
yourTextView.setText(currnetDateString);

更新:

如您所知,如果您使用inspectionDateEdit作为DatePicker,您可以使用它:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
Date currentDate = Calendar.getInstance().getTime();
String currnetDateString = sdf.format(currentDate);
try {
  Date currentDate2 = sdf.parse(currnetDateString);
  inspectionDateEdit.setDate(currentDate2);
} catch (ParseException e) {
  e.printStackTrace();
}
© www.soinside.com 2019 - 2024. All rights reserved.