Отображение времени и даты

1

Как показать дату и время без java или Picker, так же просто, как TimeView

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

и я получу время или дату с устройства. Выбор времени/даты занимает много места для показа.

Теги:

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 файлах, настроив его с помощью параметров StandView 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>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню