本文介绍了在Android的EDITTEXT的onClick Datepickerdialog错误的棒棒糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用datepickerdialog。它奇巧正常运行,但是当我在棒棒糖上运行的应用程序,当我点击编辑文本它会打开一个datepickerdialog盒子,但是当我选择日期它送给不幸停止错误。下面是code的日期选择器上的EditText。
私人无效setDateTimeField(){
fromLabel.setOnClickListener(本);
toLabel.setOnClickListener(本);
最后的DateFormat DATEFORMAT =新的SimpleDateFormat(DD-MM-YYYY); // YYYY / MM / DD HH:MM:SS
最后日期日期=新的日期();
最后弦乐U = dateFormat.format(日期);
日历newCalendar = Calendar.getInstance();
fromDatePickerDialog =新DatePickerDialog(这一点,新OnDateSetListener(){
公共无效onDateSet(DatePicker的观点,年整型,诠释monthOfYear,诠释DAYOFMONTH){
日历newDate = Calendar.getInstance();
newDate.set(年,monthOfYear,DAYOFMONTH);
1跳转= dateFormatter.format(newDate.getTime());
DIFF1 = newDate.getTimeInMillis();
长D = date.getTime();
如果((newDate.getTime())。等于(日期)||(newDate.getTime())。之后(日期)){
长D1 =(DIFF1 /(24 * 60 * 60 * 1000) - D /(24 * 60 * 60 * 1000))+ 1;
如果(D1→30){
total.setVisibility(View.VISIBLE);
total.setText(预订不允许作为给定的日期是外提前预订期);
avail.setVisibility(View.GONE);
}
其他{
total.setVisibility(View.GONE);
fromLabel.setText(1跳转);
toLabel.setText(空);
以= NULL;
avail.setVisibility(View.GONE);
从= fromLabel.getText()的toString();
}
}
其他{
total.setVisibility(View.VISIBLE);
total.setText(后,选择日期或等于当前日期);
fromLabel.setText();
toLabel.setText(空);
从=无效;
以= NULL;
}
}
},newCalendar.get(Calendar.YEAR),newCalendar.get(Calendar.MONTH),newCalendar.get(Calendar.DAY_OF_MONTH));
fromDatePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,的getString(R.string.Done),新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
如果(这== DialogInterface.BUTTON_POSITIVE){
dialog.cancel();
如果(type.equals(按时间))
{
INT CNT = -1;
如果(from1.equals(U)){
CNT = 1;
loadTimeSpinnerDataATT(文字,从,CNT);
}
否则,如果(total.getText()==后,选择日期或等于当前日期)
{
}
否则,如果(total.getText()==预订不允许作为给定的日期是提前预订期外)
{
}
其他
{CNT = 0;
loadTimeSpinnerDataATT(文字,从,CNT);
}
}
}
}
});
}
公共无效的onClick(视图查看){
如果(查看== fromLabel){
fromDatePickerDialog.show();
}否则,如果(查看== toLabel){
toDatePickerDialog.show();
}
}
公共无效的OnClose(DialogInterface dialogInterface)
{
}
}
解决方案
试试看,可以帮助你,
这里日期选取器错误resloved
进口java.text.ParseException;
进口java.text.SimpleDateFormat的;
进口的java.util.Calendar;
进口java.util.Date;
进口android.support.v7.app.ActionBarActivity;
进口android.text.InputType;
进口android.app.DatePickerDialog;
进口android.app.Dialog;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.DatePicker;
进口android.widget.EditText;
公共类MainActivity扩展ActionBarActivity {
私人诠释一年;
私人诠释一个月;
私人诠释天;
静态最终诠释DATE_PICKER_ID = 1111;
//为日期选择器
的EditText m3_DateDisplay;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
m3_DateDisplay =(EditText上)findViewById(R.id.datepick);
//获取当前日期日历
最后的日历C = Calendar.getInstance();
年= c.get(Calendar.YEAR);
月= c.get(Calendar.MONTH);
天= c.get(Calendar.DAY_OF_MONTH);
//显示选定的日期
StringBuilder的dateValue1 =新的StringBuilder()追加(天).append( - )。
。.append(月+ 1).append( - )追加(年).append();
//转换为正确的日期格式保存到数据库
SimpleDateFormat的sdf123 =新的SimpleDateFormat(DD-MM-YYYY);
串ABS1 = dateValue1.toString();
日期testDate1 = NULL;
尝试 {
testDate1 = sdf123.parse(ABS1);
}赶上(ParseException的E){
e.printStackTrace();
}
SimpleDateFormat的formatter1 =新的SimpleDateFormat(DD-MM-YYYY);
日期格式字符串= formatter1.format(testDate1);
m3_DateDisplay.setText(日期格式);
m3_DateDisplay.setFocusable(假);
m3_DateDisplay.setInputType(InputType.TYPE_NULL);
m3_DateDisplay.setOnClickListener(新View.OnClickListener(){
@燮pressWarnings(德precation)
@覆盖
公共无效的onClick(视图v){
的ShowDialog(DATE_PICKER_ID);
}
});
}
@覆盖
受保护的对话框onCreateDialog(INT ID){
开关(ID){
案例DATE_PICKER_ID:
//打开日期选择器对话框。
//设置日期选取器的当前日期
//添加pickerListener听者到日期选择器
//返回新DatePickerDialog(这一点,pickerListener,年,月,
// 日);
// ///只显示到日期不多说。
DatePickerDialog对话框=新DatePickerDialog(这一点,
pickerListener,年,月,日);
。dialog.getDatePicker()setMaxDate(新日期()的getTime());
返回对话框;
}
返回null;
}
私人DatePickerDialog.OnDateSetListener pickerListener =新DatePickerDialog.OnDateSetListener(){
//当关闭对话框,如下方法将被调用。
@覆盖
公共无效onDateSet(DatePicker的观点,诠释selectedYear,
INT selectedMonth,诠释selectedDay){
年= selectedYear;
月= selectedMonth;
天= selectedDay;
//显示选定的日期
StringBuilder的DATEVALUE =新的StringBuilder()。追加(天)
.append( - )。追加(月+ 1).append( - )。追加(年)
。附加( );
//转换为正确的日期格式保存到数据库
SimpleDateFormat的sdf123 =新的SimpleDateFormat(DD-MM-YYYY);
串ABS1 = dateValue.toString();
日期testDate1 = NULL;
尝试 {
testDate1 = sdf123.parse(ABS1);
}赶上(ParseException的E){
e.printStackTrace();
}
SimpleDateFormat的formatter1 =新的SimpleDateFormat(DD-MM-YYYY);
日期格式字符串= formatter1.format(testDate1);
m3_DateDisplay.setText(日期格式);
}
};
}
变化最小的API-11在清单
I am using datepickerdialog. it run properly on kitkat but when i run application on lollipop and when i click on edit text it opens a datepickerdialog box but when i select date it give unfortunately stop error. Below is the code for datepicker on edittext.
private void setDateTimeField() {
fromLabel.setOnClickListener(this);
toLabel.setOnClickListener(this);
final DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); //yyyy/MM/dd HH:mm:ss
final Date date = new Date();
final String u = dateFormat.format(date);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
from1 = dateFormatter.format(newDate.getTime());
diff1 = newDate.getTimeInMillis();
long d = date.getTime();
if((newDate.getTime()).equals(date)||(newDate.getTime()).after(date)){
long d1 = (diff1 / (24 * 60 * 60 * 1000) - d / (24 * 60 * 60 * 1000)) + 1;
if(d1>30){
total.setVisibility(View.VISIBLE);
total.setText("Booking not allowed as the Date given is outside Advance Booking Period");
avail.setVisibility(View.GONE);
}
else{
total.setVisibility(View.GONE);
fromLabel.setText(from1);
toLabel.setText(null);
to=null;
avail.setVisibility(View.GONE);
from=fromLabel.getText().toString();
}
}
else{
total.setVisibility(View.VISIBLE);
total.setText("Choose date after or equals to current date");
fromLabel.setText("");
toLabel.setText(null);
from=null;
to=null;
}
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
fromDatePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.Done), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
dialog.cancel();
if(type.equals("According to time"))
{
int cnt=-1;
if(from1.equals(u)){
cnt = 1;
loadTimeSpinnerDataATT(text,from,cnt);
}
else if(total.getText()=="Choose date after or equals to current date")
{
}
else if(total.getText()=="Booking not allowed as the Date given is outside Advance Booking Period")
{
}
else
{cnt = 0;
loadTimeSpinnerDataATT(text,from,cnt);
}
}
}
}
});
}
public void onClick(View view) {
if(view == fromLabel) {
fromDatePickerDialog.show();
} else if(view == toLabel) {
toDatePickerDialog.show();
}
}
public void onClose(DialogInterface dialogInterface)
{
}
}
解决方案
Try it, may help you,
Date picker error resloved here
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.support.v7.app.ActionBarActivity;
import android.text.InputType;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private int year;
private int month;
private int day;
static final int DATE_PICKER_ID = 1111;
// for date picker
EditText m3_DateDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m3_DateDisplay = (EditText) findViewById(R.id.datepick);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// Show selected date
StringBuilder dateValue1 = new StringBuilder().append(day).append("-")
.append(month + 1).append("-").append(year).append(" ");
// for Converting Correct Date format Save into Database
SimpleDateFormat sdf123 = new SimpleDateFormat("dd-MM-yyyy");
String abs1 = dateValue1.toString();
Date testDate1 = null;
try {
testDate1 = sdf123.parse(abs1);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy");
String DateFormat = formatter1.format(testDate1);
m3_DateDisplay.setText(DateFormat);
m3_DateDisplay.setFocusable(false);
m3_DateDisplay.setInputType(InputType.TYPE_NULL);
m3_DateDisplay.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
showDialog(DATE_PICKER_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker
// return new DatePickerDialog(this, pickerListener, year, month,
// day);
// ///Only Show till Date Not More than That.
DatePickerDialog dialog = new DatePickerDialog(this,
pickerListener, year, month, day);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
StringBuilder dateValue = new StringBuilder().append(day)
.append("-").append(month + 1).append("-").append(year)
.append(" ");
// for Converting Correct Date format Save into Database
SimpleDateFormat sdf123 = new SimpleDateFormat("dd-MM-yyyy");
String abs1 = dateValue.toString();
Date testDate1 = null;
try {
testDate1 = sdf123.parse(abs1);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy");
String DateFormat = formatter1.format(testDate1);
m3_DateDisplay.setText(DateFormat);
}
};
}
change minimum api-11 in manifest
这篇关于在Android的EDITTEXT的onClick Datepickerdialog错误的棒棒糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!