您的位置:首页 > 移动开发 > Android开发

Android控件之TimePicker、DatePicker

2015-12-05 16:31 441 查看
1、TimePicker、DatePicker简介

在前两篇文章中,使用的是弹出框的形式用来,展示时间和日期,而TimePicker、DatePicker控件则是将时间和日期显示在布局上。效果如下



2、代码

这样的时间和日期控件就需要在xml布局文件中,编写这两个控件相关的布局代码,使用TimePicker、DatePicker标签。具体如下

<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"
tools:context="com.example.layout_timepicker.MainActivity">
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_below="@+id/timePicker1"
android:onClick="setTime"
android:text="Button" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/datePicker1"
android:layout_below="@+id/datePicker1"
android:onClick="setDate"
android:text="Button" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
java代码实现了,点击按钮后,弹出相应的时间设置,java代码如下

package com.example.layout_timepicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

private TimePicker timePicker;
private DatePicker datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = (TimePicker) findViewById(R.id.timePicker1);
datePicker = (DatePicker) findViewById(R.id.datePicker1);
}

public void setTime(View view){
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
Toast.makeText(this, hour + ":" + minute, Toast.LENGTH_SHORT).show();
}
public void setDate(View view){
int year = datePicker.getYear();
int month = datePicker.getMonth();
int day = datePicker.getDayOfMonth();
Toast.makeText(this, year + "年" + month + "月" + day + "日", Toast.LENGTH_SHORT).show();
}
@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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: