AppCompatSpinner的条目在第二次选择时未打开TimePickerDialog。请查看详细信息
摘要: super(context;&listener;super.setSelection(position);˃parent?// Todo Auto-generated method stub;listener.onnothingSelected(parent)
如何解决AppCompatSpinner的条目在第二次选择时未打开TimePickerDialog。请查看详细信息?
跟着这些步骤:
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;
}
}
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);
解决方法
我将以下内容作为AppCompatSpinner
条目:
- Now
- Pick a time..
选择后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并选择时间。
我来说两句