作为一个初学者项目(我是编程新手),我想创建一个简单的应用程序,它将显示四个TextView:当前时间(小时和分钟),日期,月份日期和年份。它似乎相对简单,我研究了下面的代码是我想出的。有人能够解释为什么我的应用程序部队关闭?我没有在可用权限列表中看到任何有关读取系统时间的内容。提前致谢。 (注意:这是我正在使用的唯一类和布局。)
MainActivity.java:
package press.linx.calendar;
import android.os.Bundle;
import android.app.Activity;
import android.text.format.Time;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView day = (TextView)findViewById(R.id.day);
TextView month = (TextView)findViewById(R.id.month);
TextView year = (TextView)findViewById(R.id.year);
TextView time = (TextView)findViewById(R.id.Time);
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
day.setText(today.monthDay); // Day of the month (0-31)
month.setText(today.month); // Month (0-11)
year.setText(today.year); // Year
time.setText(today.format("%k:%M:%S")); // Current time
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="89dp"
android:layout_marginTop="32dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/day"
android:layout_marginRight="74dp"
android:layout_marginTop="96dp"
android:text="TextView" />
<TextView
android:id="@+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/day"
android:layout_below="@+id/Time"
android:layout_marginLeft="38dp"
android:layout_marginTop="71dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/month"
android:layout_below="@+id/month"
android:layout_marginTop="64dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
答案 0 :(得分:1)
我收到你的错误。
today.monthDay
给出一个整数值,当您在TextView
中设置它时,它会搜索具有该id
的资源。您必须获得Resources$NotFoundException
。
试试这个:
day.setText("" + today.monthDay); // Day of the month (0-31)
month.setText("" + today.month); // Month (0-11)
year.setText("" + today.year); // Year
time.setText("" + today.format("%k:%M:%S")); // Current time
答案 1 :(得分:0)
您可以将Java util和SimpleDateFormat和Date用于相同的目的:
long time = System.currentTimeMillis();
// Day of the month (0-31)
SimpleDateFormat sdf = new SimpleDateFormat("d");
String day = sdf.format(new Date(time));
day.setText(day);
// month
sdf = new SimpleDateFormat("M");
String month = sdf.format(new Date(time));
month.setText(month);
// year
sdf = new SimpleDateFormat("y");
String year = sdf.format(new Date(time));
year.setText(year);
// time
sdf = new SimpleDateFormat("K:mm:ss a");
String time = sdf.format(new Date(time));
time.setText(time);
所有时间字符串格式都可以在http://developer.android.com/reference/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String)
找到答案 2 :(得分:-1)
使用此java utils方法检索系统当前日期和时间。
Time dtNow = new Time();
dtNow.setToNow();
String sytem_time_date = dtNow.format("%Y-%m-%d %H:%M:%S"); //use single text view to show