我正在开发一个应用程序,我必须通过单击图像按钮并在TextView中显示它来选择有利时间(从 - 到时间)。 但是,当我单击图像按钮时,我的应用程序强制关闭。这是使用的代码。
代码:
public class AddClassExperiment extends AppCompatActivity implements View.OnClickListener {
EditText a_course_code, a_subject;
TextView a_from_time, a_to_time;
Spinner a_shift_spinner, a_room_spinner, day_selector;
ImageButton a_from_tym_image, a_to_tym_image;
Button save_d_day, delete_d_day;
private Integer hr, min;
static final int TIME_DIALOG_ID = 1;
static final int TIME_DIALOG_ID2 = 2;
int cur = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_class);
a_course_code = (EditText) findViewById(R.id.year_edittext);
a_subject = (EditText) findViewById(R.id.subj_edittext);
a_shift_spinner = (Spinner) findViewById(R.id.shift_spinner);
a_room_spinner = (Spinner) findViewById(R.id.room_spinner);
a_from_time = (TextView) findViewById(R.id.fromtym_textView);
a_to_time = (TextView) findViewById(R.id.totym_textView);
day_selector = (Spinner) findViewById(R.id.day_spinner);
save_d_day = (Button) findViewById(R.id.save_button);
delete_d_day = (Button) findViewById(R.id.delete_button);
a_from_tym_image = (ImageButton) findViewById(R.id.fromtym_image);
a_to_tym_image = (ImageButton) findViewById(R.id.totym_image);
a_from_tym_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
a_to_tym_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID2);
}
});
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
cur = TIME_DIALOG_ID;
return new TimePickerDialog(this, timePickerListener, hr, min, false);
case TIME_DIALOG_ID2:
cur = TIME_DIALOG_ID2;
return new TimePickerDialog(this, timePickerListener, hr, min, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hr = hourOfDay;
min = minute;
String am_pm;
if (hourOfDay > 12) {
hr = hourOfDay - 12;
am_pm = "PM";
} else {
hr = hourOfDay;
am_pm = "AM";
}
if (cur == TIME_DIALOG_ID) {
a_from_time.setText(new StringBuilder().append(hr).append(":").append(min).append(am_pm));
}
else {
a_to_time.setText(new StringBuilder().append(hr).append(":").append(min).append(am_pm));
}
}
};
@Override
public void onClick(View v) {showDialog(0);}}
我可能使用了不必要的代码,但是当我在不使用TIME_DIALOG_ID的情况下使用代码时它工作正常,但两个图像按钮的时间都显示在同一个textView中。 请帮我解决这个问题,并建议我一个简单的方法。
答案 0 :(得分:0)
确定不错但请尝试此代码
////////////////////// 公共类Timepicker扩展了Activity {
TextView mDate, mTime, mTime1, mTime2;
int pHour, pMinute;
int pHour1, pMinute1;
int pHour2, pMinute2;
static final int TIME_DIALOG_ID = 0;
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
pHour = hourOfDay;
pMinute = minute;
displayToast();
updateDisplay();
}
};
private void updateDisplay() {
mTime.setText(
new StringBuilder()
.append(pad(pHour)).append(":")
.append(pad(pMinute)));
}
private void displayToast() {
Toast.makeText(this, new StringBuilder().append("Time choosen is ")
.append(mTime.getText()), Toast.LENGTH_SHORT).show();
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
static final int TIME_DIALOG_ID1 = 1;
private TimePickerDialog.OnTimeSetListener mTimeSetListener1 =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view1, int hourOfDay, int minute) {
pHour1 = hourOfDay;
pMinute1 = minute;
displayToast1();
updateDisplay1();
}
};
private void updateDisplay1() {
mTime1.setText(
new StringBuilder()
.append(pad1(pHour1)).append(":")
.append(pad1(pMinute1)));
}
private void displayToast1() {
Toast.makeText(this, new StringBuilder().append("Time choosen is ")
.append(mTime1.getText()), Toast.LENGTH_SHORT).show();
}
private static String pad1(int c1) {
if (c1 >= 10)
return String.valueOf(c1);
else
return "0" + String.valueOf(c1);
}
static final int TIME_DIALOG_ID2 = 2;
private TimePickerDialog.OnTimeSetListener mTimeSetListener2 =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view2, int hourOfDay, int minute) {
pHour2 = hourOfDay;
pMinute2 = minute;
displayToast2();
updateDisplay2();
}
};
private void updateDisplay2() {
mTime2.setText(
new StringBuilder()
.append(pad2(pHour2)).append(":")
.append(pad2(pMinute2)));
}
private void displayToast2() {
Toast.makeText(this, new StringBuilder().append("Time choosen is ")
.append(mTime2.getText()), Toast.LENGTH_SHORT).show();
}
private static String pad2(int c2) {
if (c2 >= 10)
return String.valueOf(c2);
else
return "0" + String.valueOf(c2);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timepicker);
mTime = (TextView) findViewById(R.id.pickATime);
mTime1 = (TextView) findViewById(R.id.pickATime1);
mTime2 = (TextView) findViewById(R.id.pickATime2);
mTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
mTime1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID1);
}
});
mTime2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID2);
}
});
final Calendar cal = Calendar.getInstance();
pHour = cal.get(Calendar.HOUR_OF_DAY);
pMinute = cal.get(Calendar.MINUTE);
protected Dialog onCreateDialog(int id) {
if (id == TIME_DIALOG_ID) {
return new TimePickerDialog(this,
mTimeSetListener, pHour, pMinute, false);
} else if (id == TIME_DIALOG_ID1) {
return new TimePickerDialog(this,
mTimeSetListener1, pHour1, pMinute1, false);
} else if (id == TIME_DIALOG_ID2) {
return new TimePickerDialog(this,
mTimeSetListener2, pHour2, pMinute2, false);
}
return null;
}
}