1

如何在没有 java 或 Picker 的情况下显示日期和时间,就像这样简单TimeView

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

我会从设备上获取时间或日期。时间/日期选择器占据了很大的展示空间。

4

1 回答 1

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 文件中使用它,并使用标准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>
于 2011-12-09T22:10:43.233 回答