AppCompatSpinner的条目在第二次select时不打开TimePickerDialog。 请看详情

我有以下作为AppCompatSpinner的条目:

 <string-array name="startTimeList"> <item>Now</item> <item>Pick a time..</item> </string-array> 

一旦selectPick a time.. ,一个TimePickerDialog被打开,用户被允许select一个时间。 就是这样:

 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) { } }); 

在第一次selectPick a time..的时候,TimePickerDialog被成功打开并显示所选的时间,但是当我再次select它或者再次单击时,没有任何反应!

我不知道为什么!

请让我知道如何打开TimePickerDialog并select时间,无论我select/点击它多less次。

Solutions Collecting From Web of "AppCompatSpinner的条目在第二次select时不打开TimePickerDialog。 请看详情"

按着这些次序:

创build一个自定义的微调类

  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(); } @Override public 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() { @Override public 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); } } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub if (listener != null) { listener.onNothingSelected(parent); } } }); } public void setOnItemSelectedEvenIfUnchangedListener( OnItemSelectedListener listener) { this.listener = listener; } } 

在您的活动中创buildonItemSelectedListener

 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); 

在你的xml文件

 <your_package_name.SpinnerOnSameSelection android:id="@+id/startTimeSpinner" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:entries="@array/mList"> </your_package_name.SpinnerOnSameSelection> 

微调(或实际上AdapterView,它的超类)只有当select改变时才触发OnItemSelectedListener。

当用户select“select一个时间…”时,Spinner会认为它是一个选定的选项,如果再次select,则不再调用监听器。 但是,您可以注意到,如果select“select一个时间…”,则select“现在”,然后“select一个时间…”,出现对话框。

我会build议不要在你的情况下使用微调(和做一些奇怪的狗屎与选定的看法)。 您可以简单地使用显示选定时间和两个button的TextView来完成相同的操作 – 一个显示对话框,一个重置时间到现在。