Java 为什么Calendar.getInstance()和getDate()方法不能正常工作?无法检索日期
我曾尝试检索Android应用程序的日期,但由于某些原因,它并没有按预期显示。我希望日期在应用程序中可见。我尝试在XML文件中使用TextView和TextClock类型(这两种类型应该产生相同的结果)。仍然没有运气。“@+id/current_real_time”确实出现在应用程序中,并且显示了实时(尽管只有小时和分钟)。但是,“@+id/当前_日期”似乎没有出现在任何地方。背景是深蓝色。所以android:textColor不是这里的问题Java 为什么Calendar.getInstance()和getDate()方法不能正常工作?无法检索日期,java,android,date,calendar,Java,Android,Date,Calendar,我曾尝试检索Android应用程序的日期,但由于某些原因,它并没有按预期显示。我希望日期在应用程序中可见。我尝试在XML文件中使用TextView和TextClock类型(这两种类型应该产生相同的结果)。仍然没有运气。“@+id/current_real_time”确实出现在应用程序中,并且显示了实时(尽管只有小时和分钟)。但是,“@+id/当前_日期”似乎没有出现在任何地方。背景是深蓝色。所以android:textColor不是这里的问题 '''' <TextClock and
''''
<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();
''''
“”
''''
''''
''''
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activated_ticket_layout);
//将逻辑与UI元素匹配(添加票据菜单)
ticketToolbar=(工具栏)findViewById(R.id.addTicketToolbar);
设置支持操作栏(ticketToolbar);
getSupportActionBar().setTitle(“添加新票据”);
ticketToolbar.setNavigationIcon(右可绘制的后箭头);
//工具栏.setNavigationIcon(R.drawable.plus);
//添加和格式化时间
日历=Calendar.getInstance();
字符串currentTime=DateFormat.getTimeInstance().format(calendar.getTime());
TextClock textClockTime=(TextClock)findViewById(R.id.current\u real\u time);
textClockTime.setText(当前时间);
Calendar ticketDate=Calendar.getInstance();
日期currentTicketDate=ticketDate.getDate();
''''
因为我不知道您使用的是哪种布局,我假设您使用的是LinearLayout
然后,这将是TextClock布局的问题,因为“android:layout\u height=“match\u 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>
谢谢你的建议。要回答你最初的问题,可以。我正在使用LinearLayout。但是,当尝试合并你提供的代码片段时,没有任何效果。应用程序没有更改。你能显示你的XML布局吗?如果不能,我建议你暂时删除所有其他视图,以查看是否显示TextView。我认为不能下面是一些影响布局的其他视图。可以在这里找到XML文件的Src代码(作为答案发布)
Calendar ticketDate = Calendar.getInstance();
Date currentTicketDate = ticketDate.getTime();
TextView currentDate = (TextView) findViewById(R.id.current_date);
currentDate.setText(currentTicketDate.toString());
<?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>