1

Java やピッカーを使用せずに日付と時刻を表示するにはどうすればよいですか?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);
  }
}

TextViewその後、標準パラメータでカスタマイズして、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" >
  <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 に答える