跟着这些步骤:
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.AdapterView; import android.widget.Spinner; public class SpinnerOnSameSelection extends Spinner {OnItemSelectedListener listener;private AdapterView<?> lastParent;private View lastView;private long lastId;public SpinnerOnSameSelection(Context context, AttributeSet attrs) { super(context, attrs); // Todo Auto-generated constructor stub initlistner();}@Overridepublic void setSelection(int position) { if (position == getSelectedItemPosition() && listener != null) {listener.onItemSelected(lastParent, lastView, position, lastId); } else {super.setSelection(position); }}private void initlistner() { // Todo Auto-generated method stub super.setonItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // Todo Auto-generated method stub lastParent = parent; lastView = view; lastId = id; if (listener != null) {listener.onItemSelected(parent, view, position, id); }}@Overridepublic void onnothingSelected(AdapterView<?> parent) { // Todo Auto-generated method stub if (listener != null) {listener.onnothingSelected(parent); }} });}public void setonItemSelectedEvenIfUnchangedListener(OnItemSelectedListener listener) { this.listener = listener;}}
private AdapterView.OnItemSelectedListener listener; listener = new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View arg1, int i, long arg3) {//startTimeString = adapterView.getItemAtPosition(i).toString();DateFormat currentDateFormat = new SimpleDateFormat('HH:mm:ss');userAvailableTimeInSF = currentDateFormat.format(new Date());final TextView startTimeSpinnerTV = (TextView) adapterView.getSelectedView();startTimeSpinnerTV.setText(userAvailableTimeInSF);switch (i) { case 0:// userAvailableTimeInSF = currentDateFormat.format(new Date());startTimeSpinnerTV.setText('Now');break; case 1:final Calendar c = Calendar.getInstance();mHour = c.get(Calendar.HOUR_OF_DAY);mMinute = c.get(Calendar.MINUTE);TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) {Calendar date = Calendar.getInstance();date.set(Calendar.HOUR_OF_DAY, hourOfDay);date.set(Calendar.MINUTE, minute);date.set(Calendar.AM_PM, date.get(Calendar.AM_PM));//showTime(hourOfDay, minute); /* userAvailableTimeInSF = new SimpleDateFormat('HH:mm:ss').format(date.getTime());startTimeSpinnerTV.setText(userAvailableTimeAMPM);*/Toast.makeText(getBaseContext(), 'userAvailableTimeInSF: ' + userAvailableTimeInSF, Toast.LENGTH_SHORT).show(); }}, mHour, mMinute, false);timePickerDialog.show();break;} } @Override public void onnothingSelected(AdapterView<?> arg0) {// Todo Auto-generated method stub }};}
SpinnerOnSameSelection startTimeSpinner;in onCreate(): startTimeSpinner = (SpinnerOnSameSelection) findViewById(R.id.idOfItemInXml);startTimeSpinner.setonItemSelectedEvenIfUnchangedListener(listener);
<your_package_name.SpinnerOnSameSelection android: android:layout_width='match_parent' android:layout_height='40dp' android:layout_alignParentBottom='true' android:entries='@array/mList'></your_package_name.SpinnerOnSameSelection>解决方法
我将以下内容作为AppCompatSpinner条目:
<string-array name='startTimeList'> <item>Now</item> <item>Pick a time..</item></string-array>
选择后Pick a time..,将TimePickerDialog打开a并允许用户选择时间。这是如何做:
startTimeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView,View view,int i,long l) {//startTimeString = adapterView.getItemAtPosition(i).toString();DateFormat currentDateFormat = new SimpleDateFormat('HH:mm:ss');userAvailableTimeInSF = currentDateFormat.format(new Date());final TextView startTimeSpinnerTV = (TextView) adapterView.getSelectedView();startTimeSpinnerTV.setText(userAvailableTimeInSF);switch (i) { case 0:userAvailableTimeInSF = currentDateFormat.format(new Date());startTimeSpinnerTV.setText('Now');break; default:final Calendar c = Calendar.getInstance();mHour = c.get(Calendar.HOUR_OF_DAY);mMinute = c.get(Calendar.MINUTE);TimePickerDialog timePickerDialog = new TimePickerDialog(PostSportRequest.this,new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view,int hourOfDay,int minute) {Calendar date = Calendar.getInstance();date.set(Calendar.HOUR_OF_DAY,hourOfDay);date.set(Calendar.MINUTE,minute);date.set(Calendar.AM_PM,date.get(Calendar.AM_PM));showTime(hourOfDay,minute);userAvailableTimeInSF = new SimpleDateFormat('HH:mm:ss').format(date.getTime());startTimeSpinnerTV.setText(userAvailableTimeAMPM);Toast.makeText(getBaseContext(),'userAvailableTimeInSF: ' + userAvailableTimeInSF,Toast.LENGTH_SHORT).show(); }},mHour,mMinute,false);timePickerDialog.show();break;} } @Override public void onNothingSelected(AdapterView<?> adapterView) { }});
Pick a time..第一次选择时,将成功打开TimePickerDialog并显示所选的时间,但是当我再次选择它或再次单击它时,什么也没有发生!
我不知道为什么!
请让我知道无论选择或单击多少次,如何打开TimePickerDialog并选择时间。