转--一款漂亮实用的Android开源日期控件timessquare
这个开源控件可以兼容到SDK8版本,可以自定义显示的年月日,以及时间范围,如图
如果我们只想显示两个月的日期选择区间:
final Calendar month = Calendar.getInstance(); month.add(Calendar.MONTH, 1);
然后初始化时候可以传进去一些参数
1 final CalendarPickerView dialogView = (CalendarPickerView) getLayoutInflater() 2 .inflate(R.layout.dialog, null, false); 3 if (text.getText().toString().equals("")) { 4 dialogView.init(new Date(), month.getTime()).withSelectedDate(new Date()); 6 } else { 7 Date date = DateUtils.getDate(text.getText().toString(), "yyyy-MM-dd"); 9 dialogView.init(new Date(), month.getTime()).withSelectedDate(date); 11 }
其中withSelectedDate(date)表示指定当前显示的日期,可以自定义。
具体Demo代码
1 package com.example.timesample; 2 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.GregorianCalendar; 6 7 import com.squareup.timessquare.CalendarPickerView; 8 import com.squareup.timessquare.CalendarPickerView.SelectionMode; 9 10 import android.os.Bundle; 11 import android.app.Activity; 12 import android.app.AlertDialog; 13 import android.content.DialogInterface; 14 import android.util.Log; 15 import android.view.Menu; 16 import android.view.View; 17 import android.view.View.OnClickListener; 18 import android.widget.Button; 19 import android.widget.TextView; 20 import android.widget.Toast; 21 22 public class MainActivity extends Activity { 23 24 private Button btn; 25 private TextView text; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 btn = (Button) findViewById(R.id.button1); 32 text = (TextView) findViewById(R.id.textView1); 33 34 final Calendar nextYear = Calendar.getInstance(); 35 nextYear.add(Calendar.YEAR, 1); 36 final Calendar month = Calendar.getInstance(); 37 month.add(Calendar.MONTH, 1); 38 39 btn.setOnClickListener(new OnClickListener() { 40 41 @Override 42 public void onClick(View v) { 43 // TODO Auto-generated method stub 44 45 final CalendarPickerView dialogView = (CalendarPickerView) getLayoutInflater() 46 .inflate(R.layout.dialog, null, false); 47 if (text.getText().toString().equals("")) { 48 dialogView.init(new Date(), month.getTime()) 49 .withSelectedDate(new Date()); 50 } else { 51 Date date = DateUtils.getDate(text.getText().toString(), 52 "yyyy-MM-dd"); 53 dialogView.init(new Date(), month.getTime()) 54 .withSelectedDate(date); 55 } 56 57 new AlertDialog.Builder(MainActivity.this) 58 .setTitle("时间选择控件") 59 .setView(dialogView) 60 .setNeutralButton("返回", 61 new DialogInterface.OnClickListener() { 62 @Override 63 public void onClick( 64 DialogInterface dialogInterface, 65 int i) { 66 dialogInterface.dismiss(); 67 } 68 }) 69 .setPositiveButton("确定", 70 new DialogInterface.OnClickListener() { 71 @Override 72 public void onClick( 73 DialogInterface dialogInterface, 74 int i) { 75 long sd = dialogView.getSelectedDate() 76 .getTime(); 77 Date dat = new Date(sd); 78 GregorianCalendar gc = new GregorianCalendar(); 79 gc.setTime(dat); 80 java.text.SimpleDateFormat format = new java.text.SimpleDateFormat( 81 "yyyy-MM-dd "); 82 String sb = format.format(gc.getTime()); 83 System.out.println(sb); 84 text.setText(sb); 85 dialogInterface.dismiss(); 86 } 87 }).create().show(); 88 } 89 }); 90 91 } 92 93 @Override 94 public boolean onCreateOptionsMenu(Menu menu) { 95 // Inflate the menu; this adds items to the action bar if it is present. 96 getMenuInflater().inflate(R.menu.main, menu); 97 return true; 98 } 99 100 }
当前这个简单小Demo仅仅实现了其强大功能的一小部门,timessquare时间控件还支持选择时间段等强大功能,需要用到可以看源码。
Demo下载