Android 在按钮单击中实现日期选择器并存储在编辑文本中
我是Android的初学者,正在尝试在Android中实现日期选择器对话框 为此,我正在研究此链接: 到目前为止,我已经理解了,但我的要求是,当我单击Android 在按钮单击中实现日期选择器并存储在编辑文本中,android,android-datepicker,Android,Android Datepicker,我是Android的初学者,正在尝试在Android中实现日期选择器对话框 为此,我正在研究此链接: 到目前为止,我已经理解了,但我的要求是,当我单击按钮时,我需要获取日期选择器对话框,当我设置日期选择器时,我需要在编辑文本中设置值 根据链接,我们需要扩展对话框片段,并实现DatePickerDialog.OnDateSetListener 现在,我的理解是,要实现这一目标,需要遵循以下流程: 仅使用日期选择器创建新XML 创建一个Java文件并实现日期选择器代码 在父类按钮中,单击调用Inte
按钮时,我需要获取日期选择器对话框,当我设置日期选择器时,我需要在编辑文本中设置值
根据链接,我们需要扩展对话框片段
,并实现DatePickerDialog.OnDateSetListener
现在,我的理解是,要实现这一目标,需要遵循以下流程:
仅使用日期选择器创建新XML
创建一个Java文件并实现日期选择器代码
在父类按钮中,单击调用Intent
到datepicker
类
将值传递给父类编辑文本
这是正确的方法吗?这是完整的代码
Button bt_setdate=(Button) findViewById(R.id.bt_setdate);
bt_setdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
// get the current date
}
});
和外部onCreate()
bookingdate
包含选定的日期。。
并将其添加到布局中
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
以下是完整的代码
Button bt_setdate=(Button) findViewById(R.id.bt_setdate);
bt_setdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
// get the current date
}
});
和外部onCreate()
bookingdate
包含选定的日期。。
并将其添加到布局中
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
尝试以下代码:-
DatePickerDialog.OnDateSetListener date;
Calendar myCalendar;
dob_add_emp = (TextView) findViewById(R.id.dob_add_emp);
myCalendar = Calendar.getInstance();
date = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
dob_add_emp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (dob_add_emp.getText().toString().equals(""))
{
new DatePickerDialog(AddEmployee.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar
.get(Calendar.DAY_OF_MONTH)).show();
}
else
{
String dob = dob_add_emp.getText().toString();
String[] d = dob.split("-");
new DatePickerDialog(AddEmployee.this, date, Integer.parseInt(d[0]), Integer.parseInt(d[1]) - 1, Integer.parseInt(d[2]))
.show();
}
}
});
@SuppressLint("SimpleDateFormat")
private void updateLabel()
{
try
{
// String myFormat = "MM/dd/yyyy"; // In which you need put here
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date current_date = new Date();
Date session_date = sdf.parse(sdf.format(myCalendar.getTime()));
int difference = current_date.compareTo(session_date);
if (difference == 0)
{
dob_add_emp.setText(sdf.format(myCalendar.getTime()));
}
else if (difference < 0)
{
Toast.makeText(getBaseContext(), "Select date before current date", Toast.LENGTH_LONG).show();
}
else
{
dob_add_emp.setText(sdf.format(myCalendar.getTime()));
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
DatePickerDialog.OnDateSetListener日期;
日历我的日历;
dob_add_emp=(TextView)findViewById(R.id.dob_add_emp);
myCalendar=Calendar.getInstance();
日期=新的DatePickerDialog.OnDateSetListener()
{
@凌驾
公共void onDateSet(日期选择器视图,整数年,整数月,整数月)
{
//TODO自动生成的方法存根
myCalendar.set(Calendar.YEAR,YEAR);
myCalendar.set(Calendar.MONTH,monthOfYear);
myCalendar.set(Calendar.DAY\u OF\u MONTH,dayOfMonth);
updateLabel();
}
};
dob_add_emp.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
//TODO自动生成的方法存根
if(dob_add_emp.getText().toString().equals(“”)
{
新建DatePickerDialog(AddEmployee.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar
.get(Calendar.DAY/u/u MONTH)).show();
}
其他的
{
字符串dob=dob_add_emp.getText().toString();
字符串[]d=dob.split(“-”);
新建DatePickerDialog(AddEmployee.this,date,Integer.parseInt(d[0]),Integer.parseInt(d[1])-1,Integer.parseInt(d[2]))
.show();
}
}
});
@SuppressLint(“SimpleDataFormat”)
私有void updateLabel()
{
尝试
{
//字符串myFormat=“MM/dd/yyyy”;//您需要将其放在此处
DateFormat sdf=新的简化格式(“yyyy-MM-dd”);
日期当前日期=新日期();
datesession_Date=sdf.parse(sdf.format(myCalendar.getTime());
int difference=当前日期。与(会话日期)相比;
如果(差==0)
{
dob_add_emp.setText(sdf.format(myCalendar.getTime());
}
否则如果(差异<0)
{
Toast.makeText(getBaseContext(),“选择当前日期之前的日期”,Toast.LENGTH_LONG.show();
}
其他的
{
dob_add_emp.setText(sdf.format(myCalendar.getTime());
}
}
捕获(例外e)
{
//TODO:处理异常
e、 printStackTrace();
}
}
尝试以下代码:-
DatePickerDialog.OnDateSetListener date;
Calendar myCalendar;
dob_add_emp = (TextView) findViewById(R.id.dob_add_emp);
myCalendar = Calendar.getInstance();
date = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
dob_add_emp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (dob_add_emp.getText().toString().equals(""))
{
new DatePickerDialog(AddEmployee.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar
.get(Calendar.DAY_OF_MONTH)).show();
}
else
{
String dob = dob_add_emp.getText().toString();
String[] d = dob.split("-");
new DatePickerDialog(AddEmployee.this, date, Integer.parseInt(d[0]), Integer.parseInt(d[1]) - 1, Integer.parseInt(d[2]))
.show();
}
}
});
@SuppressLint("SimpleDateFormat")
private void updateLabel()
{
try
{
// String myFormat = "MM/dd/yyyy"; // In which you need put here
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date current_date = new Date();
Date session_date = sdf.parse(sdf.format(myCalendar.getTime()));
int difference = current_date.compareTo(session_date);
if (difference == 0)
{
dob_add_emp.setText(sdf.format(myCalendar.getTime()));
}
else if (difference < 0)
{
Toast.makeText(getBaseContext(), "Select date before current date", Toast.LENGTH_LONG).show();
}
else
{
dob_add_emp.setText(sdf.format(myCalendar.getTime()));
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
DatePickerDialog.OnDateSetListener日期;
日历我的日历;
dob_add_emp=(TextView)findViewById(R.id.dob_add_emp);
myCalendar=Calendar.getInstance();
日期=新的DatePickerDialog.OnDateSetListener()
{
@凌驾
公共void onDateSet(日期选择器视图,整数年,整数月,整数月)
{
//TODO自动生成的方法存根
myCalendar.set(Calendar.YEAR,YEAR);
myCalendar.set(Calendar.MONTH,monthOfYear);
myCalendar.set(Calendar.DAY\u OF\u MONTH,dayOfMonth);
updateLabel();
}
};
dob_add_emp.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
//TODO自动生成的方法存根
if(dob_add_emp.getText().toString().equals(“”)
{
新建DatePickerDialog(AddEmployee.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar
.get(Calendar.DAY/u/u MONTH)).show();
}
其他的
{
字符串dob=dob_add_emp.getText().toString();
字符串[]d=dob.split(“-”);
新建DatePickerDialog(AddEmployee.this,date,Integer.parseInt(d[0]),Integer.parseInt(d[1])-1,Integer.parseInt(d[2]))
.show();
}
}
});
@SuppressLint(“SimpleDataFormat”)
私有void updateLabel()
{
尝试
{
//字符串myFormat=“MM/dd/yyyy”;//您需要将其放在此处
DateFormat sdf=新的简化格式(“yyyy-MM-dd”);
日期当前日期=新日期();
datesession_Date=sdf.parse(sdf.format(myCalendar.getTime());
int difference=当前日期。与(会话日期)相比;
如果(差==0)
{
dob_add_emp.setText(sdf.format(myCalendar.getTime());
}
否则如果(差异<0)
{
Toast.makeText(getBaseContext(),“选择当前日期之前的日期”,Toast.LENGTH_LONG.show();
}
其他的
{
dob_add_emp.setText(sdf.format(myCalendar.getTime());
}
}
捕获(例外e)
{
//TODO:处理异常
e、 printStackTrace();
}
}
您好,请使用要打开日期选择器对话框的“点击”按钮上的代码
static final int DATE_DIALOG_ID = 999;
private int year;
private int month;
private int day;
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
btnDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
将以下方法添加到活动中,如下所示:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
public DatePickerDialog.OnDateSetListener datePickerListener =
new DatePickerDialog. OnDateSetListener(){
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into edittext
txtDate.setText(new StringBuilder().append(month + 1).append("-")
.append(day).append("-").append(year).append(" "));
}
};
您可以将日期格式化为您的招聘信息。您好,请使用要打开“日期选择器”对话框的“点击”按钮上的代码
static final int DATE_DIALOG_ID = 999;
private int year;
private int month;
private int day;
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
btnDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
package com.androidexample.datepicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class DatePickerExample extends Activity {
private TextView Output;
private Button changeDate;
private int year;
private int month;
private int day;
static final int DATE_PICKER_ID = 1111;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Output = (TextView) findViewById(R.id.Output);
changeDate = (Button) findViewById(R.id.changeDate);
// 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 current date
Output.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// Button listener to show date picker dialog
changeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// On button click show datepicker dialog
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);
}
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
Output.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
}
将以下方法添加到活动中,如下所示:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
public DatePickerDialog.OnDateSetListener datePickerListener =
new DatePickerDialog. OnDateSetListener(){
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into edittext
txtDate.setText(new StringBuilder().append(month + 1).append("-")
.append(day).append("-").append(year).append(" "));
}
};
您可以将日期格式化为您的重新招聘
package com.androidexample.datepicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class DatePickerExample extends Activity {
private TextView Output;
private Button changeDate;
private int year;
private int month;
private int day;
static final int DATE_PICKER_ID = 1111;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Output = (TextView) findViewById(R.id.Output);
changeDate = (Button) findViewById(R.id.changeDate);
// 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 current date
Output.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// Button listener to show date picker dialog
changeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// On button click show datepicker dialog
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);
}
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
Output.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/changeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click To Change Date" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current/Selected Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/Output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>