当前位置:操作系统 > 安卓/Android >>

Android编程之TimePickerDialog设置确定键导致OnTimeSetListener不响应问题

在调用TimePickerDialog时,如果要修改TimePickerDialog中确定键的名字,就需要调用setButton(int whichButton,CharSequence text,OnClickListener listener)方法,重新设置确定键的名字。但这样直接设置,例如下面的代码,就会造成OnTimeSetListener中onTimeSet不响应。

[java] 
TimePickerDialog timePickerDialog = new TimePickerDialog(this, 
                    new OnTimeSetListener() { 
                        @Override 
                        public void onTimeSet(TimePicker view, int hourOfDay, 
                                int minute) { 
                            System.out.println("time:" + hourOfDay + ":" 
                                    + minute); 
                        } 
                    }, calendar.get(Calendar.HOUR_OF_DAY), 
                    calendar.get(Calendar.MINUTE), false); 
 
            timePickerDialog.setTitle(R.string.title); 
            timePickerDialog.setButton(TimePickerDialog.BUTTON_POSITIVE, 
                    getString(R.string.ok), 
                    new DialogInte易做图ce.OnClickListener() { 
                        public void onClick(DialogInte易做图ce dialog, int which) { 
 
                        } 
                    }); 
            timePickerDialog.show(); 

所以,这里要改一下setButton(int whichButton, CharSequence text,OnClickListener listener)中,最后传入的参数,由OnClickListener listener改为TimePickerDialog对象本事,这样就可以响应OnTimeSetListener中onTimeSet的方法。代码如下:

[java]
TimePickerDialog timePickerDialog = new TimePickerDialog(this, 
        new OnTimeSetListener() { 
            @Override 
            public void onTimeSet(TimePicker view, int hourOfDay, 
                    int minute) { 
                System.out.println("time:" + hourOfDay + ":" 
                        + minute); 
            } 
        }, calendar.get(Calendar.HOUR_OF_DAY), 
        calendar.get(Calendar.MINUTE), false); 
 
timePickerDialog.setTitle(R.string.title); 
timePickerDialog.setButton(TimePickerDialog.BUTTON_POSITIVE, 
        getString(R.string.ok), timePickerDialog); 
timePickerDialog.show(); 

注意:setButton最后一个参数,不是OnClickListener对

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,