Android 从DatePicker按钮获取值时遇到困难?
我有一个按钮显示DatePicker。当我点击按钮时,它将显示DatePickerDialog。在这里,我试图获取日期值,我想在文本视图中获取它。但是,当我试图获取价值时,我面临着困难。如何在TextView中打印日期值 这是我的密码Android 从DatePicker按钮获取值时遇到困难?,android,datepicker,Android,Datepicker,我有一个按钮显示DatePicker。当我点击按钮时,它将显示DatePickerDialog。在这里,我试图获取日期值,我想在文本视图中获取它。但是,当我试图获取价值时,我面临着困难。如何在TextView中打印日期值 这是我的密码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tex
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(EditText) findViewById(R.id.editText1);
text2=(EditText) findViewById(R.id.editText2);
text3=(EditText) findViewById(R.id.editText3);
text4=(EditText) findViewById(R.id.editText4);
date=(Button) findViewById(R.id.datepicker);
date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, mDateSetListener, myear, mmonth, mday);
dpd.show();
}
});
final Calendar c = Calendar.getInstance();
myear = c.get(Calendar.YEAR);
mmonth = c.get(Calendar.MONTH);
mday = c.get(Calendar.DAY_OF_MONTH);
}
private void updateDisplay() {
// TODO Auto-generated method stub
textdate.setText(new StringBuilder()
.append(mmonth+1).append("-").append(mday).append("-")
.append(myear));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myear=year;
mmonth=monthOfYear;
mday=dayOfMonth;
updateDisplay();
}
};
在任何你想显示选择器的按钮上调用这个
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
显示选择器后,您将有“确定”按钮
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog d = new DatePickerDialog(getActivity(), this,
year, month, day);
DatePicker dp = d.getDatePicker();
dp.setMaxDate(c.getTimeInMillis());
// Create a new instance of DatePickerDialog and return it
return d;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
tv1.setText(day + "-" + month + "-" + year);
}
}
关于日期设置方法,请参考文本视图,此代码可能会帮助您解决问题 试试这个
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@SuppressLint("SimpleDateFormat")
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
pYear = selectedYear;
pMonth = selectedMonth;
pDay = selectedDay;
// Show selected date
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
// saving date to String variable
String formatedDate = sdf.format(new java.util.Date(pYear-1900, pMonth, pDay));
// setting date to TextView
// **Note :** Make sure that you have defined value to your textview i.e. textview must not null...
txt_date.setText(formatedDate);
}
};
您尚未初始化textdate
创建一个全局变量textdate并像
textdate = (TextView) findViewById(R.id.text_view);
其中text\u view是文本视图的id,就像您在编辑文本和按钮时一样您发现了什么问题?请注意,当我点击按钮时,它会显示DatePicker对话框。但是,如果我选择任何日期,如果我给出完成,那么应用程序将停止工作。它显示updateDisplay方法中的错误。