Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使用
作者:lqh 发布时间:2022-09-08 18:56:36
标签:Android,CalendarView,DatePicker,TimePicker
Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使用
简单复习下基础UI组件,做个简单的总结,Android的这些组件封装的特别好,基本套上就能使用,当然,这个减轻了开发者的负担!不过如果想要深入研究,这里面还是有很大的空间值得深度分析!简单的几个例子!仅供参考:
不多说,先上效果图:
CalendarView
ChooseView
NumberPicker
CalendarView代码区 :
main.xml代码区:CalendarView组件的使用加上一些简单的属性即可!
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择您的生日:"/>
<!-- 设置以星期二作为每周第一天
设置该组件总共显示4个星期
并对该组件的日期时间进行了定制 -->
<CalendarView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:firstDayOfWeek="3"
android:shownWeekCount="4"
android:selectedWeekBackgroundColor="#aff"
android:focusedMonthDateColor="#f00"
android:weekSeparatorLineColor="#ff0"
android:unfocusedMonthDateColor="#f9f"
android:id="@+id/calendarView" />
</LinearLayout>
Activity区代码:
public class MainActivity extends Activity
{
CalendarView cv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cv = (CalendarView)findViewById(R.id.calendarView);
// 为CalendarView组件的日期改变事件添加事件 *
cv.setOnDateChangeListener(new OnDateChangeListener()
{
@Override
public void onSelectedDayChange(CalendarView view, int year,
int month, int dayOfMonth)
{
// 使用Toast显示用户选择的日期
Toast.makeText(MainActivity.this,
"你生日是" + year + "年" + month + "月"
+ dayOfMonth + "日",
Toast.LENGTH_SHORT).show();
}
});
}
}
DatePicker,TimePicker,结合Calerdar的使用,可以供用户选择日期时使用:
代码区:
main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择购买本书的具体时间"/>
<!-- 定义一个DatePicker组件 -->
<DatePicker android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:startYear="2000"
android:endYear="2016"
android:calendarViewShown="true"
android:spinnersShown="true"/>
<!-- 定义一个TimePicker组件 -->
<TimePicker android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"/>
<!-- 显示用户输入日期、时间的控件 -->
<EditText android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"/>
</LinearLayout>
Activity代码:
public class MainActivity extends Activity
{
// 定义5个记录当前时间的变量
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatePicker datePicker = (DatePicker)
findViewById(R.id.datePicker);
TimePicker timePicker = (TimePicker)
findViewById(R.id.timePicker);
// 获取当前的年、月、日、小时、分钟
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
// 初始化DatePicker组件,初始化时指定 *
datePicker.init(year, month, day, new OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker arg0, int year
, int month, int day)
{
MainActivity.this.year = year;
MainActivity.this.month = month;
MainActivity.this.day = day;
// 显示当前日期、时间
showDate(year, month, day, hour, minute);
}
});
timePicker.setEnabled(true);
// 为TimePicker指定 *
timePicker.setOnTimeChangedListener(new OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker view
, int hourOfDay, int minute)
{
MainActivity.this.hour = hourOfDay;
MainActivity.this.minute = minute;
// 显示当前日期、时间
showDate(year, month, day, hour, minute);
}
});
}
// 定义在EditText中显示当前日期、时间的方法
private void showDate(int year, int month
, int day, int hour, int minute)
{
EditText show = (EditText) findViewById(R.id.show);
show.setText("您的购买日期为:" + year + "年"
+ (month + 1) + "月" + day + "日 "
+ hour + "时" + minute + "分");
}
}
NumberPicker主要使用在给用户提供数字选择时使用。
main.xml代码:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="选择低价:"
android:layout_width="120dp"
android:layout_height="wrap_content" />
<NumberPicker
android:id="@+id/np1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:focusable="true"
android:focusableInTouchMode="true" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="选择高价:"
android:layout_width="120dp"
android:layout_height="wrap_content" />
<NumberPicker
android:id="@+id/np2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:focusable="true"
android:focusableInTouchMode="true" />
</TableRow>
</TableLayout>
Activity代码:
public class MainActivity extends Activity
{
NumberPicker np1, np2;
// 定义最低价格、最高价格的初始值
int minPrice = 25, maxPrice = 75;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
np1 = (NumberPicker) findViewById(R.id.np1);
// 设置np1的最小值和最大值
np1.setMinValue(10);
np1.setMaxValue(50);
// 设置np1的当前值
np1.setValue(minPrice);
np1.setOnValueChangedListener(new OnValueChangeListener()
{
// 当NumberPicker的值发生改变时,将会激发该方法
@Override
public void onValueChange(NumberPicker picker,
int oldVal, int newVal)
{
minPrice = newVal;
showSelectedPrice();
}
});
np2 = (NumberPicker) findViewById(R.id.np2);
// 设置np2的最小值和最大值
np2.setMinValue(60);
np2.setMaxValue(100);
// 设置np2的当前值
np2.setValue(maxPrice);
np2.setOnValueChangedListener(new OnValueChangeListener()
{
// 当NumberPicker的值发生改变时,将会激发该方法
@Override
public void onValueChange(NumberPicker picker, int oldVal,
int newVal)
{
maxPrice = newVal;
showSelectedPrice();
}
});
}
private void showSelectedPrice()
{
Toast.makeText(this, "您选择最低价格为:" + minPrice
+ ",最高价格为:" + maxPrice, Toast.LENGTH_SHORT)
.show();
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
0
投稿
猜你喜欢
- 知识点:1.使用SQL Helper创建数据库2.数据的增删查改(PRDU:Put、Read、Delete、Update)背景知识:上篇文章
- 我有以下课程public class ModInfo : IEquatable<ModInfo>{ public int ID
- 经常有同学问到,使用Android能不能开发游戏呢?能开发那些游戏呢?由于操作系统和开发语言局限,一般开发安卓手机游戏,我们很少使用其自带语
- 如下所示:import java.util.ArrayList;import com.heima.bean.Person;public cl
- 前言Queue 也是 Java 集合框架中定义的一种接口,直接继承自 Collection 接口。除了基本的 Collection 接口规定
- 目的:在使用mybatis框架中mapper文件有自动生成,但有时需要自己添加sql语句进行开发,当遇到需要使用 if进行条件判断的时候该怎
- 1. openFeign实现基于spring-boot-starter-parent 2.6.8,spring-cloud-dependen
- 这个问题困扰了很久,有些类不是controller在使用autowired注入的类显示为空,找到网上的方法是在类初始化时主动注入被Autow
- 简单工厂模式介绍:概要:简单工厂模式,又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模
- 使用到的类:net.sf.json.JSONObject 使用JSON时,除了要导入JSON网站上面下载的json-lib-2.2
- 本文实例总结了C# XML序列化方法及常用特性。分享给大家供大家参考,具体如下:C#对象XML序列化(一):序列化方法和常用特性.Net F
- 前言该篇介绍的内容如题,就是利用redis实现接口的限流( 某时间范围内 最大的访问次数 ) 。正文 惯例,
- 本文实例为大家分享了Java实现在线聊天室的具体代码,供大家参考,具体内容如下- 本文讲述了从实现单个多个客户的收发信息(基础简易版),到各
- 本文实例讲述了Java实现的傅里叶变化算法。分享给大家供大家参考,具体如下:用JAVA实现傅里叶变化 结果为复数形式 a+bi废话不多说,实
- 在使用fastJson时,对于泛型的反序列化很多场景下都会使用到TypeReference,例如:void testTypeReferenc
- 本文实例讲述了WinForm实现的图片拖拽与缩放功能。分享给大家供大家参考,具体如下:最近做项目的时候遇到上传施工平面布置图,查看,因为图片
- System.Web.Caching.Cache Insert和Add方法的区别Add()object Add(string key, ob
- 在前后端分离的项目中后端返回的格式一定要友好,不然会对前端的开发人员带来很多的工作量。那么SpringBoot如何做到统一的后端返回格式呢?
- 前言最近数据库大作业要连接数据库,看了很多博客文章终于连接好了,但是没有看到一篇博客是能直接连接完成的,所以在这记录一下希望能帮助大家sql
- 前言 短时间提升自己最快的手段就是背面试题,最近总结了Java常用的面试题,分享给大家,希望大家都能圆梦大厂,加油,我命由我不由天