我有警报对话框,其中包括无线电组,我的问题是如何启用和禁用单选按钮,当我点击一个单选按钮时,它应该禁用直到,单击下一个单选按钮。请帮帮我。
public class AddDriverStateDialog extends DialogFragment {
private static AddDriverStateDialog addDriverStateDialog;
// Data references
private int DRIVER_STATE = 0;
private Calendar calendar;
private int year, month, day, hours, minutes;
private SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy");
private SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
// UI References
private View view;
public LinearLayout editableDetails;
private RadioRealButtonGroup radioButtonGroup;
private RadioRealButton radioButton_offduty,radioButton_onduty,radioButton_sleeper,radioButton_driving,radioButton_yard,radioButton_personal;
private EditText note, et_date, time, odoReading, location;
private CheckBox insertLog, switchDriver;
private Spinner spinner;
private MainActivity mainActivity;
// Picker Dialogs
private DatePickerDialog datePickerDialog;
private TimePickerDialog timePickerDialog;
public static AddDriverStateDialog newInstance(MainActivity mainActivity) {
if (addDriverStateDialog == null) {
addDriverStateDialog = new AddDriverStateDialog();
}
addDriverStateDialog.mainActivity = mainActivity;
return addDriverStateDialog;
}
@Override
public void onResume() {
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
super.onResume();
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialise UI elements
view = LayoutInflater.from(mainActivity).inflate(R.layout.dialog_add_driver_state, null);
editableDetails = (LinearLayout) view.findViewById(R.id.ll_addDriverState_editableDetails);
radioButtonGroup = (RadioRealButtonGroup) view.findViewById(R.id.radioGroup_dialog_addDriverState_driverStates);
note = (EditText) view.findViewById(R.id.et_dialog_addDriverState_note);
et_date = (EditText) view.findViewById(R.id.et_dialog_addDriverState_date);
time = (EditText) view.findViewById(R.id.et_dialog_addDriverState_time);
odoReading = (EditText) view.findViewById(R.id.et_dialog_addDriverState_odometer);
location = (EditText) view.findViewById(R.id.et_dialog_addDriverState_location);
insertLog = (CheckBox) view.findViewById(R.id.chkBox_addDriverStare_insertLog);
switchDriver = (CheckBox) view.findViewById(R.id.chkBox_addDriverStare_switchDriver);
radioButton_offduty = (RadioRealButton) view.findViewById(R.id.radioBtn_dialog_addDriverState_offDuty);
radioButton_onduty=(RadioRealButton) view.findViewById(R.id.radioBtn_dialog_addDriverState_onDuty);
radioButton_sleeper=(RadioRealButton) view.findViewById(R.id.radioBtn_dialog_addDriverState_sleeperBerth);
radioButton_driving=(RadioRealButton) view.findViewById(R.id.radioBtn_dialog_addDriverState_driving);
radioButton_yard=(RadioRealButton) view.findViewById(R.id.radioBtn_dialog_addDriverState_yard_move);
radioButton_personal=(RadioRealButton) view.findViewById(R.id.radioBtn_dialog_addDriverState_personal_conveyance);
// Initialise Data references
calendar = Calendar.getInstance();
// Get current dateTime and time
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DATE);
hours = calendar.get(Calendar.HOUR);
minutes = calendar.get(Calendar.MINUTE);
// Initialise data references
datePickerDialog = new DatePickerDialog(mainActivity, 0, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(year, month, dayOfMonth);
et_date.setText(dateFormat.format(calendar.getTime()));
}
}, year, month, day);
timePickerDialog = new TimePickerDialog(mainActivity, 0, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
time.setText(timeFormat.format(calendar.getTime()));
}
}, hours, minutes, false);
// UI Interaction listeners
/* Driver State Selection Listener */
radioButtonGroup.setOnClickedButtonListener(new RadioRealButtonGroup.OnClickedButtonListener() {
@Override
public void onClickedButton(RadioRealButton button, int position) {
switch (button.getId()) {
case R.id.radioBtn_dialog_addDriverState_offDuty:
DRIVER_STATE = DriverLog.STATE_OFFLINE;
break;
case R.id.radioBtn_dialog_addDriverState_onDuty:
DRIVER_STATE = DriverLog.STATE_ONLINE;
break;
case R.id.radioBtn_dialog_addDriverState_sleeperBerth:
DRIVER_STATE = DriverLog.STATE_SLEEPER;
break;
case R.id.radioBtn_dialog_addDriverState_driving:
DRIVER_STATE = DriverLog.STATE_DRIVING;
break;
case R.id.radioBtn_dialog_addDriverState_yard_move:
DRIVER_STATE = DriverLog.STATE_YARD_MOVE;
break;
case R.id.radioBtn_dialog_addDriverState_personal_conveyance:
DRIVER_STATE = DriverLog.STATE_PERSONAL_CONVEYANCE;
break;
}
}
});
et_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePickerDialog.show();
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timePickerDialog.show();
}
});
insertLog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
editableDetails.setVisibility(View.VISIBLE);
} else {
editableDetails.setVisibility(View.GONE);
}
}
});
switchDriver.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
radioButton_onduty.setVisibility(View.GONE);
radioButton_driving.setVisibility(View.GONE);
radioButton_yard.setVisibility(View.GONE);
radioButton_personal.setVisibility(View.GONE);
radioButtonGroup.setOnClickedButtonListener(new RadioRealButtonGroup.OnClickedButtonListener() {
@Override
public void onClickedButton(RadioRealButton button, int position) {
switch (button.getId()) {
case R.id.radioBtn_dialog_addDriverState_offDuty:
button.setVisibility(View.VISIBLE);
DRIVER_STATE = DriverLog.STATE_OFFLINE;
break;
case R.id.radioBtn_dialog_addDriverState_sleeperBerth:
button.setVisibility(View.VISIBLE);
DRIVER_STATE = DriverLog.STATE_SLEEPER;
break;
}
}
});
}
}
});
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(mainActivity)
.setTitle("Update Driver Log")
.setCancelable(false)
.setView(view)
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mainActivity.updateDashboard(DRIVER_STATE, note.getText().toString());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
})
.create();
}
}
答案 0 :(得分:0)
所以要做你想做的事情:
radioButtonGroup.setOnClickedButtonListener(new RadioRealButtonGroup.OnClickedButtonListener() {
@Override
public void onClickedButton(RadioRealButton button, int position) {
switch (button.getId()) {
case R.id.radioBtn_dialog_addDriverState_offDuty:
DRIVER_STATE = DriverLog.STATE_OFFLINE;
radioButton_offduty.setEnabled(false);
radioButton_onduty.setEnabled(true);
radioButton_sleeper.setEnabled(true);
radioButton_driving.setEnabled(true);
radioButton_yard.setEnabled(true);
radioButton_personal.setEnabled(true);
break;
case R.id.radioBtn_dialog_addDriverState_onDuty:
DRIVER_STATE = DriverLog.STATE_ONLINE;
radioButton_offduty.setEnabled(true);
radioButton_onduty.setEnabled(false);
radioButton_sleeper.setEnabled(true);
radioButton_driving.setEnabled(true);
radioButton_yard.setEnabled(true);
radioButton_personal.setEnabled(true);
break;
case R.id.radioBtn_dialog_addDriverState_sleeperBerth:
DRIVER_STATE = DriverLog.STATE_SLEEPER;
radioButton_offduty.setEnabled(true);
radioButton_onduty.setEnabled(true);
radioButton_sleeper.setEnabled(false);
radioButton_driving.setEnabled(true);
radioButton_yard.setEnabled(true);
radioButton_personal.setEnabled(true);
break;
case R.id.radioBtn_dialog_addDriverState_driving:
DRIVER_STATE = DriverLog.STATE_DRIVING;
radioButton_offduty.setEnabled(true);
radioButton_onduty.setEnabled(true);
radioButton_sleeper.setEnabled(true);
radioButton_driving.setEnabled(false);
radioButton_yard.setEnabled(true);
radioButton_personal.setEnabled(true);
break;
case R.id.radioBtn_dialog_addDriverState_yard_move:
DRIVER_STATE = DriverLog.STATE_YARD_MOVE;
radioButton_offduty.setEnabled(true);
radioButton_onduty.setEnabled(true);
radioButton_sleeper.setEnabled(true);
radioButton_driving.setEnabled(true);
radioButton_yard.setEnabled(false);
radioButton_personal.setEnabled(true);
break;
case R.id.radioBtn_dialog_addDriverState_personal_conveyance:
DRIVER_STATE = DriverLog.STATE_PERSONAL_CONVEYANCE;
radioButton_offduty.setEnabled(true);
radioButton_onduty.setEnabled(true);
radioButton_sleeper.setEnabled(true);
radioButton_driving.setEnabled(true);
radioButton_yard.setEnabled(true);
radioButton_personal.setEnabled(false);
break;
}
}
});