android *** Layout 10 DatePicker && TimePicker
2016-02-01 18:53
423 查看
照例贴地址,http://android-doc.com/guide/topics/ui/controls/pickers.html
主要还是监听器的设置。
xml文件如下
java如下
TimePicker一样,只是设置监听器的问题而已
xml
java如下
主要还是监听器的设置。
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"; @Override protected 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() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub String 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-->>"); } @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); } @Override public 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"; @Override protected 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(){ @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub String 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-->>"); } @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); } @Override public void onClick(View v) { // TODO Auto-generated method stub } }
相关文章推荐
- Android自定义View控件
- Android 显示获取服务器的超大图片 <19>
- android *** Layout 09 CheckBox
- Android之判断某个服务是否正在运行的方法
- android 进程优先级
- Android之Proguard语法
- android一个弹出菜单的动画(二)
- Glide 3.0 介绍
- TabLayout实现仿网易新闻客户端Tab标签
- Android APK瘦身之Android Studio Lint (代码审查)
- 1.Android JUnit Runner(使用AndroidStudio)
- 介绍一款Android小游戏--交互式人机对战五子棋
- Android中include标签的使用
- android 多媒体数据库详解
- 制作可独立分发的Android模拟器
- android 控件布局
- Android 文件缓存方法
- android Monkey 测试技巧
- android CheckBox RadioButton 图片与文字间距问题
- 浅谈Android系统开发中LOG的使用