在下图中,我使用spannable字符串来获取这样的显示。当我点击时间(12:00 PM)时,将弹出时间选择器对话框并设置时间。但是当我为第一次设定时间时,第二次也会改变。怎么避免这个? 下面是我写的以下代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.cancel_appointment_dialog, container, false);
mCancellationReasonTextInput = (TextInputLayout) rootView.findViewById(R.id.edit_text_cancellation_field);
mCancelAllRadioButton = (RadioButton) rootView.findViewById(R.id.cancel_appointment_radio_button);
mCancelFromRadioButton = (RadioButton) rootView.findViewById(R.id.cancel_appointment_from_radio_button);
mCancelBetweenRadioButton = (RadioButton) rootView.findViewById(R.id.cancel_appointment_between_radio_button);
mBlockSlotsCheckBox = (CheckBox) rootView.findViewById(R.id.block_cancelled_slots_check_box);
mNotifyPatientsCheckBox = (CheckBox) rootView.findViewById(R.id.notify_patients_check_box);
mOkButton = (Button) rootView.findViewById(R.id.ok_button);
showSpannableStringBetween();
mCancelFromRadioButton.setText(mSpannableStringFrom);
mCancelFromRadioButton.setMovementMethod(LinkMovementMethod.getInstance());
mOkButton.setOnClickListener(onOKButtonClick);
return rootView;
}
void showSpannableStringBetween() {
String titleText = " 12:00 PM";
String sampleText = " and" + titleText;
String resultText = getString(R.string.cancel_between_text) + titleText; //61
mSpannableFirstString = new SpannableString(resultText);
mSpannableFirstString.setSpan(new StyleSpan(Typeface.BOLD), (resultText.length() - titleText.length()), resultText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableFirstString.setSpan(mClickableSpanBetween, (resultText.length() - titleText.length()), resultText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableFirstString.setSpan(new ForegroundColorSpan(Color.BLACK), (resultText.length() - titleText.length()), resultText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableSecondString = new SpannableString(sampleText);
mSpannableSecondString.setSpan(new StyleSpan(Typeface.BOLD), (sampleText.length() - titleText.length()), sampleText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableSecondString.setSpan(mClickableSpanBetween, (sampleText.length() - titleText.length()), sampleText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableSecondString.setSpan(new ForegroundColorSpan(Color.BLACK), (sampleText.length() - titleText.length()), sampleText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mCancelBetweenRadioButton.setText(TextUtils.concat(mSpannableFirstString , mSpannableSecondString));
mCancelBetweenRadioButton.setMovementMethod(LinkMovementMethod.getInstance());
}
ClickableSpan mClickableSpanBetween = new ClickableSpan() {
@Override
public void onClick(View widget) {
int hour = getHour();
int minute = getMinutes();
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String format;
setHour(selectedHour);
setMinutes(selectedMinute);
if (selectedHour == 0) {
selectedHour += 12;
format = "AM";
} else if (selectedHour == 12) {
format = "PM";
} else if (selectedHour > 12) {
selectedHour -= 12;
format = "PM";
} else {
format = "AM";
}
String timeText = selectedHour + ":" + selectedMinute + " " + format;
String titleText = getString(R.string.cancel_from_text) + " " + timeText + " and " + timeText;
mSpannableFirstString = new SpannableString(titleText);
mSpannableFirstString.setSpan(new StyleSpan(Typeface.BOLD), 46,53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableFirstString.setSpan(mClickableSpanBetween, 46, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableFirstString.setSpan(new ForegroundColorSpan(Color.BLACK), 46, 53, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableSecondString.setSpan(new StyleSpan(Typeface.BOLD), (titleText.length() - timeText.length()), titleText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableSecondString.setSpan(mClickableSpanBetween, (titleText.length() - timeText.length()), titleText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableSecondString.setSpan(new ForegroundColorSpan(Color.BLACK), (titleText.length() - timeText.length()), titleText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mCancelBetweenRadioButton.setText(mSpannableFirstString);
}
}, hour, minute, false);//Yes 24 hour time
mTimePicker.setTitle("Start Time");
mTimePicker.show();
}
};