2011-12-09 139 views
1

如何显示日期和时间没有Java或一个选择器,像那样简单TimeView时间和日期显示

android:@+id/ETC 
android:TextSize="" and so on. 

。我会从设备的时间或日期。时间/日期选择器占用很多展示空间。

回答

1

有没有内置的小工具的这一点,但你可以通过扩展TextView添加一个:

package com.example.widget; 

import java.text.SimpleDateFormat; 
import java.util.Date; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class TimeView extends TextView { 
    public TimeView(Context context) { 
    super(context); 
    updateTime(); 
    } 

    public TimeView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    updateTime(); 
    } 

    public TimeView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    updateTime(); 
    } 

    public void updateTime() { 
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String now = format.format(new Date()); 
    setText(now); 
    } 
} 

然后你可以用它在你的XML文件,与standrad TextView参数定制它:

<?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" > 
    <com.example.widget.TimeView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#0f0" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</LinearLayout>