我做了一個按鈕來打開日期和時間選擇器,但必須選中一個複選框才能激活我的按鈕。沒有複選框,它可以正常工作,chekbox選中或取消選中,它不會打開日期選擇器。你能告訴我爲什麼嗎?需要複選框以允許按下按鈕 - 如果不工作
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pick_date = (Button) findViewById(R.id.pick_date);
tv_result = (TextView) findViewById(R.id.tvdate_result);
CheckBox fixed_date_flag = (CheckBox) findViewById(R.id.fixed_date);
final boolean checked = (fixed_date_flag).isChecked();
if(checked){
pick_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(NewActivity.this, NewActivity.this, year, month, day);
datePickerDialog.show();
}
});}
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
yearFinal = year;
monthFinal = month + 1;
dayFinal = dayOfMonth;
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(NewActivity.this, NewActivity.this, hour, minute, DateFormat.is24HourFormat(this));
timePickerDialog.show();
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
hourFinal = hourOfDay;
minuteFinal = minute;
tv_result.setText(
"year: " + yearFinal + "\n" +
"month: " + monthFinal + "\n" +
"day: " + dayFinal + "\n" +
"hour: " + hourFinal + "\n" +
"minute: " + minuteFinal);
}
}
您需要在複選框上設置checkchangelistener來獲取複選框的當前狀態,只要它發生變化 –
您有調試嗎?通過調試你可以看到「checked」的值 –