android时间相关控件

来源:互联网 发布:买家被淘宝封号前预兆 编辑:程序博客网 时间:2024/05/16 17:56

1.DigitalClock控件显示时间,实时更新

<DigitalClock

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android1:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:textSize="30sp"

    android:layout_gravity="center_horizontal"/> 

过时了,建议使用TextClock

<TextClock    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/textClock"    android:layout_centerVertical="true"    android:layout_centerHorizontal="true" />


2.TimePicker选择时间:

<?xml version="1.0" encoding="utf-8"?><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"    tools:context="com.qf.zhouyi.test.MainActivity">    <TimePicker        android:id="@+id/timePic1"        android:layout_height="wrap_content"        android:layout_width="match_parent"/>    <Button        android:id="@+id/buttone1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/timePic1"        android:text="获取TimePick时间"/></RelativeLayout>

public class MainActivity extends AppCompatActivity {                private TimePicker timePick1;    private Button buttone1;      @Override      protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          timePick1=(TimePicker)findViewById(R.id.timePic1);                    buttone1=(Button)findViewById(R.id.buttone1);          OnChangeListener  buc=new OnChangeListener();          buttone1.setOnClickListener(buc);          //是否使用24小时制          timePick1.setIs24HourView(true);          TimeListener times=new TimeListener();          timePick1.setOnTimeChangedListener(times);      }            class OnChangeListener implements View.OnClickListener {        @Override          public void onClick(View v) {            // TODO Auto-generated method stub              int h=timePick1.getCurrentHour();              int m=timePick1.getCurrentMinute();              System.out.println("h:"+h+"   m:"+m);          }      }      class TimeListener implements TimePicker.OnTimeChangedListener {                  /**          * view 当前选中TimePicker控件          *  hourOfDay 当前控件选中TimePicker 的小时          * minute 当前选中控件TimePicker  的分钟          */          @Override          public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {              // TODO Auto-generated method stub              System.out.println("h:"+ hourOfDay +" m:"+minute);          }                }    } 

3.DataPicker

<DatePicker    android:id="@+id/myDatePicker"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_gravity="center_horizontal"    android:startYear="2015"    android:endYear="2020"    android:calendarViewShown="true" //是否显示右边日历    android:spinnersShown="true" //是否显示左边spinner/>


Android:Picker(DatePicker、TimerPicker、NumberPicker)

private DatePicker datePicker;private Calendar calendar;private int year;private int month;private int day;// 获取日历对象calendar = Calendar.getInstance();// 获取当前对应的年、月、日的信息year = calendar.get(Calendar.YEAR);month = calendar.get(Calendar.MONTH) + 1;day = calendar.get(Calendar.DAY_OF_MONTH);datePicker = (DatePicker) findViewById(R.id.myDatePicker);// dataPicker初始化datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {    @Override    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {        setTitle(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);    }});





原创粉丝点击