我正在使用这种方法从日历中读取日期并将其显示在TextView上,但它只显示25:10:2015,当我选择其他日期时它不会改变。我写了一个事件来改变日期,但我不知道出了什么问题: package com.example.user.calendar;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView datee=(TextView)findViewById(R.id.textView2);
final Calendar c=Calendar.getInstance();
final int year =c.get(Calendar.YEAR);
final int month =c.get(Calendar.MONTH);
final int day =c.get(Calendar.DAY_OF_MONTH);
datee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog datepicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
datee.setText(" ");
datee.setText(year + ":" + month + ":" + day);
// dayyy=datee.getText().toString();
}
}, year, month, day);
datepicker.setTitle("select date");
datepicker.show();
}
});
}
答案 0 :(得分:1)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageButton ib;
private Calendar cal;
private int day;
private int month;
private int year;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib = (ImageButton) findViewById(R.id.imageButton1);
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
et = (EditText) findViewById(R.id.editText);
ib.setOnClickListener((View.OnClickListener) this);
}
@Override
public void onClick(View v) {
showDialog(0);
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
+ selectedYear);
}
};
}
答案 1 :(得分:1)
尝试这个我希望它的工作。
private 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 textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
答案 2 :(得分:0)
当你'setText'时,你正在使用'Calendar.getInstance();'中的变量。根据当前日,月和年设置这些变量。相反,您应该使用由datePicker设置的变量。
而不是:
datee.setText(year + ":" + month + ":" + day);
使用它:
datee.setText(year + ":" + monthOfYear + ":" + dayOfMonth);
此外,Calendar.Month返回的月份是一个(10而不是11),因为Java日历从0到11。因此,您必须在月份中添加1才能获得当前月份的正确数字。但默认情况下,datePicker返回的月份值(dayOfMonth)应该是正确的。