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

天天见闻 天天见闻 2023-03-15 科技 阅读: 91
摘要: 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并选择时间。

其他相关

jquery动画效果实例_动画js

作者: 天天见闻 时间:2023-07-25 阅读: 66
开始视频<;distance;button>;var btn=document.querySelector(“button”);Box.style.left=“800px”;/script>;var Box=document.querySelector(“#Box”);function animation(target;clearInterval(Timer);...
贺红梅溪四季地产介绍

贺红梅溪四季地产介绍

作者: 天天见闻 时间:2023-03-26 阅读: 100
和尾梅市四季位于大河市仙桃区、莱梅片区中央、梅溪湖CBD核心区,未来项目现场将新增邮轮码头“,保证入住者欣赏景观。室内私密性更强;加工景观包括活动功能:lt社区大厅>;进入大厅的会所式入口高度10米;业主专用电梯直通观景台:lt品牌电梯>;。电梯净高2.5米;lt智能安防系统>。...
将所需的驱动程序包含在maven-assembly-plugin中时,找不到合适的驱动程序。

将所需的驱动程序包含在maven-assembly-plugin中时,找不到合适的驱动程序。

作者: 天天见闻 时间:2023-03-21 阅读: 116
JDBC驱动程序发现机制依赖于一个名为的文件/meta-inf/services/java.sql.Driver,maven-assembly-pluginpack.name.MainClass˂jar-with-dependencies˂11.2.0.3version˃/version˃˂...
并详细点击addEventListener和removeEventListener事件。

并详细点击addEventListener和removeEventListener事件。

作者: 天天见闻 时间:2023-03-19 阅读: 103
...
Spring WS Web服务。使用SAAJ向响应中添加附件-端点无适配器

Spring WS Web服务。使用SAAJ向响应中添加附件-端点无适配器

作者: 天天见闻 时间:2023-03-15 阅读: 84
org.springframework.ws˂invoke(@RequestPayload DownloadMessageRequestSaaj req;\\temp\\maven-feather.png"));payload.setMessagePayLoad(handler);...
妈呀!邓紫棋把成人纸尿裤穿出来了…还是换回皮裤吧!

妈呀!邓紫棋把成人纸尿裤穿出来了…还是换回皮裤吧!

作者: 天天见闻 时间:2023-01-27 阅读: 195
小妹看到这一条“像尿不湿”的评论时简直要笑翻。说实话,邓紫棋拿“港台最佳女歌手”的时候,穿的一袭金色宫廷风长裙还是很仙的:紫色外穿bra搭配渔网上衣,真是引人注意的一把好手。只是仔细看,这外穿的白色大短裤依然像是皮质的……拉远一点看,全身造型是这样的——还穿了银色恨天高的靴子?展现自我,用皮裤撑起乐坛半边天!不过上面这些也都还是玩笑话了,毕竟话说回来,邓紫棋的唱功还是有目共睹的。...
我来说两句

年度爆文