这是我到目前为止所有人。我研究了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>
MainActivity.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;
}
}
答案 0 :(得分:7)
您需要修改以下代码
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);
}
答案 1 :(得分:3)
你只是喜欢
private void Nonevent_Event() {
String stringDate=Date_Class.date_time(stringCalendar);
stringCalendar.setText(stringDate);
}
您定义了stringDate
但忘了返回值Nonevent_Event()
来存储它。
答案 2 :(得分:2)
String stringDate = "";
Date_Class.date_time(stringCalendar);
这会调用您的方法,但会忽略返回的值。你需要做
String stringDate = Date_Class.date_time(stringCalendar);
答案 3 :(得分:2)
请使用以下代码
private void Nonevent_Event()
{
String stringDate = "";
//please have a look on these lines and use it
stringDate = Date_Class.date_time(stringCalendar);
//now do the operation
stringCalendar.setText(stringDate);
}
答案 4 :(得分:0)
试试这个。
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
有时在模拟器中运行也不会返回当前日期和时间。