android 时间选择器 自己选择时间 仿小米定闹铃
源码:http://download.youkuaiyun.com/detail/lm_zp/9518943
activity_main.xml
<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"
>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择时间"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>mytimer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c0c0c0"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
<Button
android:id="@+id/button_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="确定" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_height="wrap_content" >
<NumberPicker
android:id="@+id/numpick"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/numpick"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>MainActivity.java
package com.example.mytimer;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private NumberPicker numpick;
private TimePicker time;
String[] datename = { "今天", "明天", "后天" };
private String currenttime;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//显示时间
textView = (TextView) findViewById(R.id.text);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Dialog 对话框
final Dialog dialog = new Dialog(MainActivity.this);
//dialog布局
View inflate = LayoutInflater.from(MainActivity.this).inflate(
R.layout.mytimer, null);
dialog.setContentView(inflate);
//dialog头
dialog.setTitle("选择时间");
dialog.setCanceledOnTouchOutside(true);
//显示
dialog.show();
// 找到该布局文件下的控件
time = (TimePicker) inflate.findViewById(R.id.time);
numpick = (NumberPicker) inflate.findViewById(R.id.numpick);
Button button_cancel = (Button) inflate.findViewById(R.id.button_cancel);
Button button_confirm = (Button) inflate.findViewById(R.id.button_confirm);
// 是否使用24小时制
time.setIs24HourView(true);
// 设置日期选择器的显示文字
numpick.setDisplayedValues(datename);
numpick.setMinValue(0);
numpick.setMaxValue(datename.length - 1);
// 为按钮设置点击监听事件
button_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 关闭当前的窗口
dialog.dismiss();
}
});
button_confirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 1、获取索引值
int value = numpick.getValue();
// 获取该索引值下的文字(即是今天还是明天、后天)
String dateValue = datename[value];
// 2、获取当前的分钟时间
Integer currentMinute = time.getCurrentMinute();
if(currentMinute.toString().length() == 1)
{
//当前时间
currenttime = time.getCurrentHour() + ":0"
+ time.getCurrentMinute();
}else{
currenttime = time.getCurrentHour() + ":"
+ time.getCurrentMinute();
}
//显示赋值
textView.setText(dateValue+currenttime+"");
//关闭
dialog.dismiss();
}
});
}
});
}
}
本文介绍了如何在Android应用中创建一个自定义的时间选择器,用于让用户选择时间,类似于小米闹钟的设定方式。提供了源码链接供读者下载参考,包括activity_main.xml布局文件和MainActivity.java的实现代码。
1897

被折叠的 条评论
为什么被折叠?



