Android日历控件PickTime代码实例
最近做项目,需要设置用户的生日,所以做这样一个功能。
开始发觉自带的DatePicker 很是不好用。
上代码:
<DatePicker android:id="@+id/dpPicker" android:datePickerMode="spinner" android:calendarViewShown="false" android:layout_marginTop="150dp" android:spinnersShown="true" android:yearListSelectorColor = "@color/colorPrimaryDark" android:layout_width="match_parent" android:layout_height="wrap_content" />
初始化代码:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); Calendar calendar = Calendar.getInstance(Locale.CHINA); datePicker = (DatePicker) findViewById(R.id.dpPicker); int year=calendar.get(Calendar.YEAR); int monthOfYear=calendar.get(Calendar.MONTH); int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH); datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // 获取一个日历对象,并初始化为当前选中的时间 Calendar calendar = Calendar.getInstance(); calendar.set(year, monthOfYear, dayOfMonth); SimpleDateFormat format = new SimpleDateFormat( "yyyy年MM月dd日 HH:mm"); Toast.makeText(MainActivity.this, format.format(calendar.getTime()), Toast.LENGTH_SHORT) .show(); } });
结果效果如此:
android:datePickerMode="spinner"
这个属性会切换为日历选择状态。
还容易出现英文这种鬼东西,于是不在研究,直接找插件去。
于是发现PickTime 很好用的,来说下使用方法:
//日期选择控件 implementation 'com.github.codbking:PickTime:v1.0.1' //item控件 implementation 'com.github.codbking:AllItemView:v1.0.0'
然后是调用代码:
package controller.hzl.com.timetest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; import com.codbking.view.ItemView; import com.codbking.widget.DatePickDialog; import com.codbking.widget.bean.DateType; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private DatePicker datePicker; private TimePicker timePicker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ItemView xx = findViewById(R.id.item4); xx.setOnClickListener(this); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); Calendar calendar = Calendar.getInstance(Locale.CHINA); datePicker = (DatePicker) findViewById(R.id.dpPicker); int year=calendar.get(Calendar.YEAR); int monthOfYear=calendar.get(Calendar.MONTH); int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH); datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // 获取一个日历对象,并初始化为当前选中的时间 Calendar calendar = Calendar.getInstance(); calendar.set(year, monthOfYear, dayOfMonth); SimpleDateFormat format = new SimpleDateFormat( "yyyy年MM月dd日 HH:mm"); Toast.makeText(MainActivity.this, format.format(calendar.getTime()), Toast.LENGTH_SHORT) .show(); } }); } private void showDatePickDialog(DateType type) { DatePickDialog dialog = new DatePickDialog(this); //设置上下年分限制 dialog.setYearLimt(5); //设置标题 dialog.setTitle("选择时间"); //设置类型 dialog.setType(type); //设置消息体的显示格式,日期格式 dialog.setMessageFormat("yyyy-MM-dd"); //设置选择回调 dialog.setOnChangeLisener(null); //设置点击确定按钮回调 dialog.setOnSureLisener(null); dialog.show(); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.item4: showDatePickDialog(DateType.TYPE_YMD); break; } } }
效果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。
【本文由:http://www.1234xp.com/rbzq.html 复制请保留原URL】