android *** Layout 10 DatePicker && TimePicker

来源:互联网 发布:网络安全法 翻墙 编辑:程序博客网 时间:2024/05/03 17:01

照例贴地址,http://android-doc.com/guide/topics/ui/controls/pickers.html

主要还是监听器的设置。

xml文件如下

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:android1="http://schemas.android.com/apk/res/android"    android:id="@+id/relative"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <DatePicker        android1:id="@+id/datePicker1"        android1:layout_width="wrap_content"        android1:layout_height="wrap_content"        android1:layout_alignParentLeft="true"        android1:layout_alignParentTop="true" /></RelativeLayout>

java如下

package com.example.tree;import android.support.v7.app.ActionBarActivity;import android.util.Log;import java.util.ArrayList;import java.util.Calendar;import java.util.List;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.*;import android.view.View.OnClickListener;import android.view.View.OnKeyListener;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.Button;import android.widget.CheckBox;import android.widget.DatePicker;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends ActionBarActivity implements OnClickListener{private DatePicker date;private int year,monthofyear,dayofmonth;private final String TAG="MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i(TAG, "-onCreate-->>");//初始化日期Calendar cal=Calendar.getInstance();year=cal.get(Calendar.YEAR);monthofyear=cal.get(Calendar.MONTH)+1;dayofmonth=cal.get(Calendar.DAY_OF_MONTH);date=(DatePicker)this.findViewById(R.id.datePicker1);date.init(year,monthofyear,dayofmonth, new DatePicker.OnDateChangedListener() {@Overridepublic void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {// TODO Auto-generated method stubString date=year+" year "+monthofyear+" month "+dayofmonth+" day ";Toast.makeText(MainActivity.this, "-->>"+date, 1).show();}});}protected void onStart(){super.onStart();Log.i(TAG, "-onStart-->>");}protected void onRestart(){super.onRestart();Log.i(TAG, "-onRestart-->>");}protected void onResume(){super.onResume();Log.i(TAG, "-onResume-->>");}protected void onPause(){super.onPause();Log.i(TAG, "-onPause-->>");}protected void onStop(){super.onStop();Log.i(TAG, "-onStop-->>");}protected void onDestroy(){super.onDestroy();Log.i(TAG, "-onDestroy-->>");}@Overridepublic 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;}@Overridepublic 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);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stub}}


TimePicker一样,只是设置监听器的问题而已

xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:android1="http://schemas.android.com/apk/res/android"    android:id="@+id/relative"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <TimePicker        android1:id="@+id/timePicker1"        android1:layout_width="wrap_content"        android1:layout_height="wrap_content"        android1:layout_alignParentLeft="true"        android1:layout_alignParentTop="true" /></RelativeLayout>


java如下

package com.example.tree;import android.support.v7.app.ActionBarActivity;import android.util.Log;import java.util.ArrayList;import java.util.Calendar;import java.util.List;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.*;import android.view.View.OnClickListener;import android.view.View.OnKeyListener;import android.widget.AnalogClock;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.Button;import android.widget.CheckBox;import android.widget.DatePicker;import android.widget.EditText;import android.widget.TimePicker;import android.widget.TimePicker.OnTimeChangedListener;import android.widget.Toast;public class MainActivity extends ActionBarActivity implements OnClickListener{private TimePicker time;//时间private final String TAG="MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i(TAG, "-onCreate-->>");time=(TimePicker)this.findViewById(R.id.timePicker1);time.setOnTimeChangedListener(new OnTimeChangedListener(){@Overridepublic void onTimeChanged(TimePicker view, int hourOfDay, int minute) {// TODO Auto-generated method stubString value=hourOfDay+"hour"+minute+"minute";Toast.makeText(MainActivity.this, "-->>"+value, 1).show();}});}protected void onStart(){super.onStart();Log.i(TAG, "-onStart-->>");}protected void onRestart(){super.onRestart();Log.i(TAG, "-onRestart-->>");}protected void onResume(){super.onResume();Log.i(TAG, "-onResume-->>");}protected void onPause(){super.onPause();Log.i(TAG, "-onPause-->>");}protected void onStop(){super.onStop();Log.i(TAG, "-onStop-->>");}protected void onDestroy(){super.onDestroy();Log.i(TAG, "-onDestroy-->>");}@Overridepublic 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;}@Overridepublic 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);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stub}}


0 0