安卓日期框的实现

来源:互联网 发布:linux远程访问数据库 编辑:程序博客网 时间:2024/06/05 00:13

关于安卓日期框这块内容很多人都使用过,在对其进行修改时,初步接触者需要查找一些内容,关于日期框的修改内容,目前做了一下总结,希望能一起交流讨论,也能帮助初学者。

效果如下:


这里的标题栏的内容可自定义

标题栏下的分割线颜色也可自定义

内容栏的内容也可自定义

这里日期控件的分割线颜色也可定义


代码如下:

public class DateDialog {
private Calendar calendar; // 通过Calendar获取系统时间
private Context context;
private AlertDialog alertDialog;
private DatePicker datePicker;
private StringBuffer sb;  //返回的日期字符串

private dialogCallBack diCallBack;

public interface dialogCallBack{  //日期回调
public void getDate();
}


public DateDialog(Context context) {  
this.context = context;
diCallBack=(dialogCallBack)context;
}


@SuppressLint("NewApi")
public void setDate() {
calendar = Calendar.getInstance();

//自定义的日期对话框的标题栏,可根据自己的内容进行修改
View viewTitle = LayoutInflater.from(context).inflate(
R.layout.view_title, null); 
TextView titleTv=(TextView)viewTitle.findViewById(R.id.dialog_title_tv);
titleTv.setText("设置日期");

//自定义的日期内容框的内容栏,可根据自动的内容修改,本代码中为日期控件
View view = (LinearLayout) LayoutInflater.from(context).inflate(
R.layout.dialog_date, null);
datePicker = (DatePicker) view
.findViewById(R.id.date_picker);
// 设置日期简略显示 否则详细显示 包括:星期周
datePicker.setCalendarViewShown(false);
// 初始化当前日期
calendar.setTimeInMillis(System.currentTimeMillis());
datePicker.init(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), null);

//设置日期控件中分割线的颜色
setDatePickerDividerColor(datePicker); 

// 通过自定义控件AlertDialog实现
alertDialog= new AlertDialog.Builder(context).
setCustomTitle(viewTitle).
setView(view).
setPositiveButton(context.getResources().getString(R.string.mycancel),null).
setNegativeButton(context.getResources().getString(R.string.ok),new ok()).create();

//设置对话框中标题栏下的分割线的颜色
alertDialog.setOnShowListener(new OnShowListener() {

@Override
public void onShow(DialogInterface arg0) {
// TODO Auto-generated method stub
int divierId = alertDialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);    
View divider = alertDialog.findViewById(divierId);    
divider.setBackgroundColor(context.getResources().getColor(R.color.app_pink_color)); // earlier defined color-int

}
});
alertDialog.show();
}

private class ok implements DialogInterface.OnClickListener{




@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
sb = new StringBuffer();
sb.append(String.format("%d-%02d-%02d",
datePicker.getYear(),
datePicker.getMonth() + 1,
datePicker.getDayOfMonth()));
diCallBack.getDate();

}

}

public StringBuffer getDateSb(){
return sb;


//通过反射拿到mSelectionDivider属性,然后设置上颜色值
private void setDatePickerDividerColor(DatePicker datePicker){
        // Divider changing:
          
        // 获取 mSpinners
        LinearLayout llFirst = (LinearLayout) datePicker.getChildAt(0);
         
        // 获取 NumberPicker 
        LinearLayout mSpinners = (LinearLayout) llFirst.getChildAt(0);
        for (int i = 0; i < mSpinners.getChildCount(); i++) {
            NumberPicker picker = (NumberPicker) mSpinners.getChildAt(i); 
              
            Field[] pickerFields = NumberPicker.class.getDeclaredFields();
            for (Field pf : pickerFields) {
                if (pf.getName().equals("mSelectionDivider")) {
                    pf.setAccessible(true);
                    try {
                        pf.set(picker, new ColorDrawable(context.getResources().getColor(R.color.app_pink_color)));//APP 主题颜色
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (NotFoundException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

}


Activity 中的调用

//代码只写了对话框调用的部分

public class SetDate extends Activity implements dialogCallBack{

DateDialog dialog;

//对话框的调用

dialog = new DateDialog(context);
dialog.setDate();


//对话框的回调

@Override
public void getDate() {
// TODO Auto-generated method stub
textView.setText(dialog.getDateSb());

}

}

布局文件

view_title.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="wrap_content"
     >
<TextView 
    android:id="@+id/dialog_title_tv"
    style="@style/w_full_h_wrap"
    android:gravity="center"
    android:padding="5dp"
    android:textSize="20sp"
    android:textColor="@color/app_pink_color"
    />   


</LinearLayout>

dialog_date.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:orientation="vertical"
    android:padding="10dip" >


    <DatePicker
        android:id="@+id/date_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />


</LinearLayout>

0 0