安卓app_sl3_21日期拾取器,为时间拾取器设置监听器
安卓app_sl3_21日期拾取器,为时间拾取器设置监听器
package com.example.sl3_21; import java.util.Calendar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; import android.widget.Toast; public class MainActivity extends Activity { private int year; private int month; private int day; private int hour; private int minute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DatePicker datepicker=(DatePicker)findViewById(R.id.datePicker1); TimePicker timepicker=(TimePicker)findViewById(R.id.timePicker1); timepicker.setIs24HourView(true); Calendar calendar=Calendar.getInstance(); year=calendar.get(Calendar.YEAR); month=calendar.get(Calendar.MONTH); day=calendar.get(Calendar.DAY_OF_MONTH); hour=calendar.get(Calendar.HOUR_OF_DAY); minute=calendar.get(Calendar.MINUTE); //初始化日期拾取器,并在初始化时指定监听器 datepicker.init(year, month, day, new OnDateChangedListener(){ @Override public void onDateChanged(DatePicker view, int year, int month, int day) { // TODO 自动生成的方法存根 MainActivity.this.year=year; MainActivity.this.month=month; MainActivity.this.day=day; show(year,month,day,hour,minute); } }); //为时间拾取器设置监听器 timepicker.setOnTimeChangedListener(new OnTimeChangedListener(){ @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO 自动生成的方法存根 MainActivity.this.hour=hourOfDay; MainActivity.this.minute=minute; show(year,month,day,hourOfDay,minute); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void show(int year,int month,int day,int hour,int minute) { String str=year+"年"+(month+1)+"月"+day+"日 "+hour+":"+minute; Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } }
<LinearLayout 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" android:orientation="vertical" tools:context="com.example.sl3_21.MainActivity" > <DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
欢迎讨论,相互学习。
cdtxw@foxmail.com