您的位置:首页 > 其它

Fragment中使用DatePickerDialog、TimePickerDialog

2015-06-10 10:59 337 查看
转载:http://blog.sina.com.cn/s/blog_6ba2da2d0101cft2.html

先上效果图:





上面的这个效果主要参照了 http://www.cnblogs.com/linjiqin/archive/2011/03/10/1980215.html 这个例子。但,例子中是在Activity中实现的,这里想让这个效果在Fragment中实现。

下面是我自己的代码,不知道在博客里写代码会不会变乱~

package net.zhihuiguan.www.gooutdutyv1;

import java.util.Calendar;

import android.app.DatePickerDialog;

import android.app.Dialog;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.DatePicker;

public class OutApplication extends Fragment {

privateButton dateBtn11 = null;

privateButton dateBtn12 = null;

privateButton dateBtn13 = null;

privateCalendar c = null;



@Override

public voidonCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

}

@Override

public ViewonCreateView(LayoutInflater inflater, ViewGroup container, BundlesavedInstanceState) {

// TODO Auto-generated method stub

if (container == null)

return null;

View v = inflater.inflate(R.layout.lay101, container, false);

dateBtn11 = (Button) v.findViewById(R.id.button11);

dateBtn12 = (Button) v.findViewById(R.id.button12);

dateBtn13 = (Button) v.findViewById(R.id.button13);

dateBtn11.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub



onCreateDialog(dateBtn11).show();

}

});

dateBtn12.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

onCreateDialog(dateBtn12).show();

}

});

dateBtn13.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

onCreateDialog(dateBtn13).show();

}

});

return v;

}

protectedDialog onCreateDialog(final Button btn) {

Dialog dialog = null;

c = Calendar.getInstance();

dialog = new DatePickerDialog(getActivity(), newDatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker dp, int year, int month, intdayOfMonth) {

btn.setText(year + "年" + (month + 1) + "月" + dayOfMonth +"日");

}

}, c.get(Calendar.YEAR), // 传入年份

c.get(Calendar.MONTH), // 传入月份

c.get(Calendar.DAY_OF_MONTH) // 传入天数

);



return dialog;

}

}

这么一个功能,费了我半天的时间,╮(╯▽╰)╭,有待提高啊!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: