【问题标题】:Why are Calendar.getInstance() and getDate() methods not working as intended? Cannot retrieve Date为什么 Calendar.getInstance() 和 getDate() 方法不能按预期工作?无法检索日期
【发布时间】:2019-09-03 00:59:25
【问题描述】:

我已尝试检索 Android 应用的日期,但由于某种原因,它没有按预期显示。我希望日期在应用程序中可见。我尝试在 XML 文件中使用 TextView 和 TextClock 类型(两者都应该产生相同的结果)。仍然没有运气。“@+id/current_real_time”确实出现在应用程序中并且确实显示了实时时间(尽管只有小时和分钟)。但是,“@+id/current_date”似乎没有出现在任何地方。背景是深蓝色。所以 android:textColor 不是这里的问题。

''''

<TextClock
   android:id="@+id/current_real_time"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:format12Hour="hh:mm:ss a"
   android:textColor="#ffffff"
   android:textSize="16pt"
   android:textStyle="bold"
   android:layout_marginStart="25dp">
 </TextClock>

''''

<TextView
    android:id="@+id/current_date"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text=""
    android:textSize="16pt"
    android:textColor="#ffffff"
    android:textStyle="bold" />

''''

''''
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activated_ticket_layout);


        //Matching logic to the UI element (add ticket menu)
        ticketToolbar = (Toolbar) findViewById(R.id.addTicketToolbar);
        setSupportActionBar(ticketToolbar);
        getSupportActionBar().setTitle("Add New Tickets");
        ticketToolbar.setNavigationIcon(R.drawable.back_arrow);
        //toolbar.setNavigationIcon(R.drawable.plus);

        //Adding and Formatting time
        Calendar calendar = Calendar.getInstance();
        String currentTime = DateFormat.getTimeInstance().format(calendar.getTime());
        TextClock textClockTime = (TextClock) findViewById(R.id.current_real_time);
        textClockTime.setText(currentTime);

        Calendar ticketDate = Calendar.getInstance();
        Date currentTicketDate = ticketDate.getDate();

''''

【问题讨论】:

    标签: java android date calendar


    【解决方案1】:

    由于我不知道您使用的是哪种布局,因此我假设您使用的是 LinearLayout。

    那么,就是你的TextClock布局的问题了,因为“android:layout_height="match_parent"”把TextView推到了底部,修改TextClock布局就可以使用下面的代码了

    <TextClock
        android:id="@+id/current_real_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="hh:mm:ss a"
        android:textColor="#ffffff"
        android:textSize="16pt"
        android:textStyle="bold">
    </TextClock>
    

    另外,您是否调用了 TextView 的 setText(String)?我问是因为我看不到你从上面粘贴的代码中做了什么。如果你这样做了,请忽略这个问题,或者你可以尝试下面的代码,它会显示完整的日期字符串(你可能需要自己格式化)。

        Calendar ticketDate = Calendar.getInstance();
        Date currentTicketDate = ticketDate.getTime();
    
        TextView currentDate = (TextView) findViewById(R.id.current_date);
        currentDate.setText(currentTicketDate.toString());
    

    【讨论】:

    • 感谢您的建议。要回答您最初的问题,是的。我正在使用线性布局。但是,当尝试合并您提供的代码 sn-ps 时,没有任何效果。应用没有改变。
    • 你能展示你的 XML 布局吗?如果没有,我建议您暂时删除所有其他视图以查看是否显示 TextView。我认为还有一些其他的视图会影响布局。
    • XML 文件的源代码可以在这里找到(作为答案发布)
    【解决方案2】:
        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">
    
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/addTicketToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#0066CC"
            android:minHeight="?attr/actionBarSize">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ticket"
                android:textColor="#ffffff"
                android:textSize="12pt"
                android:layout_marginStart="50dp"/>
    
    
        </android.support.v7.widget.Toolbar>
    
        <android.support.v7.widget.ActionMenuView
            android:id="@+id/amvMenu"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#0066CC">
    
    
            <TextClock
                android:id="@+id/current_real_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:format12Hour="hh:mm:ss a"
                android:textColor="#ffffff"
                android:textSize="20pt"
                android:textStyle="bold">
            </TextClock>
    
    
            <TextView
                android:id="@+id/current_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="-100dp"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="20pt"
                android:textStyle="bold" />
    
        </android.support.v7.widget.ActionMenuView>
    
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            </LinearLayout>
        </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2016-01-16
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 2018-11-15
      • 2021-05-30
      • 2020-03-05
      相关资源
      最近更新 更多