0
我是新的android,我想顯示對話框datepicker即使在TextView的點擊。但是,在textview上從datepicker設置日期需要兩次。Android DatePicker調用兩次後TextView更新
的TextView XML:
<TextView
android:id="@+id/dateTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="MM/DD/YYYY">
活動代碼:
private static final String TAG = "MainActivity";
private TextView mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setting date at the start of the app
mDisplayDate = (TextView) findViewById(R.id.dateTxtView);
mDisplayDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mCalendar = Calendar.getInstance();
int year = mCalendar.get(Calendar.YEAR);
int month = mCalendar.get(Calendar.MONTH);
int day = mCalendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dateDialog = new DatePickerDialog(
MainActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year,month,day
);
dateDialog.setTitle("Select the date");
dateDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dateDialog.show();
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
Log.d(TAG,"date has been set");
String dateToday = month + "/" + dayOfMonth + "/" + year;
mDisplayDate.setText(dateToday);
}
};
}
});
首先,我點擊的TextView和顯示對話框日期選擇器設定日期後,然後單擊確定,有沒有值傳遞給textview。在第二次,我再次點擊並設置另一個日期,日期現在傳遞給textview。請幫我解決問題。
謝謝floydheld! – Ngigz