gpt4 book ai didi

java - 为什么我启动 android 时没有显示日期?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:07 26 4
gpt4 key购买 nike

这是我目前所拥有的。我研究了 Date_Class.java 中的命令,它说应该显示当前日期。但是当我启动 android 应用程序时,什么也没有显示。为什么是这样?我有错误的代码吗?我做错了什么?

如有任何帮助,我们将不胜感激。谢谢。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- Header -->

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:textColor="#FFFFFF"
android:gravity="center"
android:layout_marginTop="20dp"
android:id="@+id/Header"
android:text=""
/>

<!-- Name -->

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:id="@+id/Name"
android:text=""
/>

<!-- Project Name -->

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:id="@+id/ProjectName"
android:text=""
/>

<!-- Digital Clock -->

<DigitalClock
android:id="@+id/DigitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text=""
/>

<!-- Calendar -->

<TextView
android:id="@+id/stringCalendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text=""
/>

<!-- End of Program -->

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#000000"
android:gravity="center"
android:id="@+id/EndOfProgram"
android:text=""
/>

</LinearLayout>

主 Activity .java

package com.example.d1project1;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.widget.DigitalClock;

import android.widget.TextView;

@SuppressWarnings({ "deprecation", "unused" })
public class MainActivity extends Activity {
//Declare GUI objects

TextView stringHeader;
TextView stringName;
TextView stringProject;
TextView digitalClock1;
TextView stringEnd;

TextView stringCalendar;
TextView stringDisplay;
DigitalClock DigitalClock;

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

Init_GUI();
DigitalClock();
Nonevent_Event();
}

private void DigitalClock() {
//DigitalClock
DigitalClock = (DigitalClock)findViewById(R.id.DigitalClock);
}
private void Nonevent_Event() {
String stringDate = "";

Date_Class.date_time(stringCalendar);

stringCalendar.setText(stringDate);
}

//method to link GUI items to code
private void Init_GUI() {
stringHeader = (TextView)findViewById(R.id.Header);
stringName = (TextView)findViewById(R.id.Name);
stringProject = (TextView)findViewById(R.id.ProjectName);
stringEnd = (TextView)findViewById(R.id.EndOfProgram);

stringCalendar = (TextView)findViewById(R.id.stringCalendar);

stringHeader.setText("Day 1 Project 1 - Travis Finlan");
stringName.setText("Travis Finlan");
stringProject.setText("Day 1 Project 1");
stringEnd.setText("End of Program");
}
}

Date_Class.java

package com.example.d1project1;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.widget.TextView;

public class Date_Class {
public static String date_time(TextView stringCalendar) {
//Calendar
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());

return formattedDate;
}
}

最佳答案

您需要修改以下代码,

private void Nonevent_Event() 
{
String stringDate = "";
Date_Class.date_time(stringCalendar);
stringCalendar.setText(stringDate); // here you forgot to assign the string value
}

到下面的代码,

private void Nonevent_Event() 
{
String stringDate = "";
stringDate = Date_Class.date_time(stringCalendar);
stringCalendar.setText(stringDate);
}

关于java - 为什么我启动 android 时没有显示日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com