Android Studio 在“onTimeSet”方法中设置按钮文本后,如何访问按钮文本?

9rygscc1  于 2022-12-23  发布在  Android
关注(0)|答案(1)|浏览(125)

我的代码

timePickerBTN.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        TimePickerDialog timePickerDialog =  new TimePickerDialog(NewTaskActivity.this, new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker timePicker, int hourOfDay, int Minute) {
                TimeHour = hourOfDay;
                TimeMinute = Minute;

                Calendar calendarTime = Calendar.getInstance();
                calendarTime.set(0,0,0, TimeHour, TimeMinute);
                timePickerBTN.setText(android.text.format.DateFormat.format("hh:mm aa", calendarTime));

            }

        },12,0,false);

        timePickerDialog.updateTime(TimeHour, TimeMinute);
        timePickerDialog.show();

    }

});

我想在设置 * timePickerBTN文本后访问它 * timePickerBTN. setText(android. text. format. DateFormat. format("hh:mm aa",calendarTime)); * 在OnTimeSet中**
我想将timePickerBTN文本(拾取时间之后)设置为TextView文本
(EDIT)尝试此方法太多enter image description here

wooyq4lh

wooyq4lh1#

使用getText()获取Button的文本,即

String text = timePickerBTN.getText().toString();

您的代码如下所示

timePickerBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                TimePickerDialog timePickerDialog =  new TimePickerDialog(NewTaskActivity.this, new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker timePicker, int hourOfDay, int Minute) {
                        TimeHour = hourOfDay;
                        TimeMinute = Minute;

                        Calendar calendarTime = Calendar.getInstance();
                        calendarTime.set(0,0,0, TimeHour, TimeMinute);
                        timePickerBTN.setText(android.text.format.DateFormat.format("hh:mm aa", calendarTime));
                        text = timePickerBTN.getText().toString();

                    }

                },12,0,false);

                timePickerDialog.updateTime(TimeHour, TimeMinute);
                timePickerDialog.show();

            }

        });

相关问题