在editText字段中显示timePicker的结果

时间:2014-01-11 05:02:02

标签: android eclipse android-edittext timepicker android-timepicker

我似乎无法将timePicker的结果导入editText字段。我按照Android培训指南中的示例进行操作,我可以在日志中看到timePicker的结果,但我无法弄清楚如何在文本字段(tvEndTime)中显示它们。

public class Setup extends Activity {

....

private final TextView tvEndTime = (TextView) findViewById(R.id.textViewEndTime);

....

public static class TimePickerFragment extends DialogFragment
    implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
        DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        Log.i(TAG, Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
        tvEndTime.setText(Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
    }
}

// show the end time timePicker dialog box fragment when user clicks buttonEndTime
public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker");
}  

....

tvEndTime.setText()行,我收到Eclipse错误“无法对非静态字段tvEndTime进行静态引用”。但我无法将tvEndTime声明为静态,因为我在tvEndTime声明中得到了错误。我究竟做错了什么?

3 个答案:

答案 0 :(得分:1)

将此代码写入您正在使用的任何视图的onClick()事件中:

            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);

            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(CurrentActivity.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    editText.setText( selectedHour + ":" + selectedMinute);
                }
            }, hour, minute, true);
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

或者:

只需将文字设置为EditText的方式改为:

tvEndTime.setText(selectedHour+" : "+selectedMinute);

更正:将TextView tvEndTime作为课程Setup的类变量

答案 1 :(得分:1)

更改tvEndTime.setText参数无效。我通过在onTimeSet函数中添加tvEndTime的声明来实现它(当用户单击按钮时调用SetEndTime):

public void SetEndTime(View v) {
    Calendar mcurrentTime = Calendar.getInstance();
    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
    int minute = mcurrentTime.get(Calendar.MINUTE);

    TimePickerDialog mTimePicker;
    mTimePicker = new TimePickerDialog(Setup.this, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
            TextView tvEndTime = (TextView) findViewById(R.id.textViewEndTime);
            tvEndTime.setText( selectedHour + ":" + selectedMinute);
        }
    }, hour, minute, true);
    mTimePicker.setTitle("Select Time");
    mTimePicker.show();
}  

答案 2 :(得分:0)

对所有其他人tvEndTime.setText(String.valueof(hourOfDay))这样做是否遵循相同的方法