android开发之DatePickerDialog与TimePickerDialog的使用
文章来源:http://www.itnose.net/detail/6249821.html
通过对话框的形式,来进行选择。尤其感觉DatePickerDialog比DatePicker在用户选择交互时好的多.
DatePickerDialog与TimePickerDialog都进行了很好的封装,对于开发者来说,直接调用,非常的简单。
演示实例:
public class MainActivity extends Activity { Button button,button2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void dateClick(View v) { Calendar calendar = Calendar.getInstance(); DatePickerDialog datePickerDialog = new DatePickerDialog( MainActivity.this, new OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) { //做你想做的事情 } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); datePickerDialog.show(); } public void timeClick(View v){ Calendar calendar = Calendar.getInstance(); TimePickerDialog dialog = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker arg0, int arg1, int arg2) { //做你想做的事情 } }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true); dialog.show(); } }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" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="dateClick" android:text="点击" /> <Button android:id="@+id/but2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="timeClick" android:text="点击" /> </LinearLayout>
更多文章:http://www.itnose.net
相关推荐
Nostalgiachild 2020-11-13
韩伟佳 2020-10-09
wuleihenbang 2020-09-16
zzqLivecn 2020-07-09
chenjinlong 2020-06-10
yinbaoshiguang 2020-06-09
sgafdsg 2020-06-04
ustcrding 2020-06-03
chenjinlong 2020-06-03
AndroidGA 2020-06-01
安辉 2020-05-27
绿豆饼 2020-05-26
CNETNews 2020-05-26
xilove0 2020-05-12
绿豆饼 2020-05-12
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
hqulyc 2020-05-05
lyccsu 2020-04-30