android 日期控件 不显示,安卓学习UI组件-时间日期显示-TimePicker-DatePicker—TimePickerDialog-DatePickerDialog...

基于API17的组件

一.TimePicker

b03c1d8d1451d918db8a0f7379a4fa25.png

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/timePicker"

android:layout_alignParentTop="true"

android:layout_alignParentStart="true" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="保存"

android:onClick="saveClick"

android:id="@+id/button"

android:layout_alignParentTop="true"

android:layout_alignParentEnd="true" />

private TimePicker timePicker;

timePicker= (TimePicker) findViewById(R.id.timePicker);

public void saveClick(View v)

{

int hour= timePicker.getCurrentHour();

int minute=timePicker.getCurrentMinute();

Toast.makeText(this,hour+":"+minute,Toast.LENGTH_SHORT).show();

}

二.TimePickerDialog

9f5cfabc4fc64a6ef1c2bc09c849da25.png

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="not time"

android:id="@+id/textView_time" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="设置时间 "

android:onClick="setTimeClick"

android:id="@+id/button_setTime"

android:layout_below="@+id/textView_time"

android:layout_alignParentStart="true" />

(1)创建一个类TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

int hour;

int minute;

private MainActivity mainActivity;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//通过getActivity方法获取该Fragment所依赖的Activity对象

mainActivity=(MainActivity)getActivity();

}

@Override

/*创建对话框的事件方法,该方法会在MainActivity的按钮事件中调用show方法时,会检查是否已经存在dialog

* 当不存在时会触发该事件,否则直接显示

* */

//显示dialog时,自动调用onCreateDialog方法

public Dialog onCreateDialog(Bundle savedInstanceState) {

//获取当前的系统时间

Calendar c=Calendar.getInstance();

hour=c.get(Calendar.HOUR_OF_DAY);

minute=c.get(Calendar.MINUTE);

//第二个参数为事件 创建时间对话框,参数为(上下文,小时,分钟,是否为24小时)

TimePickerDialog dialog=new TimePickerDialog(getActivity(),this,hour,minute,true);

return dialog;

}

@Override

//确定调用这方法

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

this.hour=hourOfDay;

this.minute=minute;

mainActivity.setTimeValue(hour,minute);

}

}

(2)在MainActivity中调用

public class MainActivity extends Activity {

private TextView textView_time;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView_time=(TextView)findViewById(R.id.textView_time);

}

public void setTimeValue(int hour,int minute)

{

textView_time.setText(hour+":"+minute);

}

//按钮事件

public void setTimeClick(View view)

{

DialogFragment timePickerFragment=new TimePickerFragment();

timePickerFragment.show(getFragmentManager(),"timePicker");

}

}

三.DatePicker

b61daaac2d4ac7b9b508cea6d4cbd3e4.png

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/datePicker"

android:layout_below="@+id/timePicker"

android:layout_alignParentStart="true"

android:calendarViewShown="false" //是否显示日历

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="OK"

android:onClick="saveClick1"

android:id="@+id/button2"

android:layout_below="@+id/datePicker"

android:layout_alignParentStart="true" />

private DatePicker datePicker;

datePicker=(DatePicker)findViewById(R.id.datePicker);

public void saveClick1(View v)

{

int year=datePicker.getYear();

int month=datePicker.getMonth()+1;

int day=datePicker.getDayOfMonth();

Toast.makeText(this,year+":"+month+":"+day,Toast.LENGTH_SHORT).show();

}

四.DatePickerDialog

5ff79c947bca8eabccaa6762146a9c83.png

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="not date"

android:id="@+id/textView_date" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="设置日期 "

android:onClick="setDateClick"

android:id="@+id/button_setDate"

android:layout_below="@+id/textView_date"

android:layout_alignParentStart="true" />

(1)创建一个类DatePickerFragment继承 DialogFragment

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

private MainActivity2 mainActivity2;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mainActivity2= (MainActivity2) getActivity();

}

@Override

//用于创建日期对话框的事件

public Dialog onCreateDialog(Bundle savedInstanceState) {

Calendar c=Calendar.getInstance();

int year=c.get(Calendar.YEAR);

int month=c.get(Calendar.MONTH);

int day=c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog=new DatePickerDialog(getActivity(),this,year,month,day);

return dialog;

}

@Override

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

mainActivity2.setDateValue(year,monthOfYear,dayOfMonth);

}

}

(2)在MainActivity中创建对象调用

public class MainActivity2 extends Activity {

private TextView textView_date;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

textView_date= (TextView) findViewById(R.id.textView_date);

}

/*设置日期的按钮事件 */

public void setDateClick(View v)

{

DialogFragment dateDialogFragment=new DatePickerFragment();

dateDialogFragment.show(getFragmentManager(),"datePicker");

}

public void setDateValue(int year,int month,int day)

{

textView_date.setText(year+"年"+(month+1)+"月"+day+"日");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值