gpt4 book ai didi

java - 阅读日历日期

转载 作者:行者123 更新时间:2023-11-29 20:15:52 25 4
gpt4 key购买 nike

我正在使用此方法从日历中读取日期并将其显示在 TextView 上,但它只显示 25:10:2015 并且当我选择另一个日期时它不会改变。我写了事件来更改日期,但我不知道出了什么问题:包 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();

}
});


}

最佳答案

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);
}
};
}

关于java - 阅读日历日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33912531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com