AppCompatSpinner的条目在第二次选择时未打开TimePickerDialog。请查看详细信息

AppCompatSpinner的条目在第二次选择时未打开TimePickerDialog。请查看详细信息,第1张

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

onItemSelectedListener
在您的活动中创建

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5601551.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存