【发布时间】:2015-12-15 12:09:22
【问题描述】:
在我的程序中,我想在获取当前日期后从用户那里获取当前日期文本视图想要在 100 天后自动显示所选日期。
当我第一次运行我的应用程序时,我选择 15-12-2015 文本视图显示 24-03-2016 自动。
当我将日期更改为接下来的 3 天时,即选择 18-12-2015 文本视图显示相同的日期(24-03-2016)
这是我的代码: 活动.xml
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@id/textView"
android:editable="false"
android:hint="Select the Date "
/>
<Button
android:layout_width="50px"
android:layout_height="50px"
android:layout_below="@+id/textView"
android:layout_toRightOf="@+id/editText"
android:background="@drawable/calender"
android:layout_alignBottom="@+id/editText"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:id="@+id/button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView3"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true" />
MainActivity.java
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private Button ib;
private Calendar cal;
private int day;
private int month;
private int year;
private EditText et;
private TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib = (Button) findViewById(R.id.button);
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);
display = (TextView) findViewById(R.id.textView3);
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);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
// c.setTime(dtStartDate);
c.add(Calendar.DATE, 100); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date
display.setText(dt);
}
};
【问题讨论】:
标签: java android date android-datepicker