从TimePicker将TextView设置为时间-Android [英] Set TextView to time from TimePicker - Android

查看:96
本文介绍了从TimePicker将TextView设置为时间-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在此处使用本教程来创建一个时间选择器.我可以选择合适的时间,但无法选择要在我的TextView中显示的时间.Java不是我偏爱的语言,所以这可能就是我的失败之处.

I have been using this tutorial here to create a time picker. I can select the time fine, but I can't get the time selected to show up in my TextView. Java is not my preferred language so that is probably where I am falling down.

这是我的Java代码:

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //code from: http://developer.android.com/guide/topics/ui/controls/pickers.html
    public void showTimePicker(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }

    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 - how!?
        }
    }
}

在我的XML中,我有选择时间"按钮,在单击时运行showTimePicker方法,如下所示:

In my XML I have the Choose Time button run the showTimePicker method on click like so:

android:onClick="showTimePicker"

我尝试只在onTimeSet方法内设置文本字段,但得到一个nullpointerexception.然后我想应该在onCreateDialog上对其进行初始化,但是我不知道如何使用findViewById查找实际的文本视图.我敢肯定这可能很简单,但是我在运气不好的情况下进行了上下搜索.感谢您的帮助!

I have tried just setting the text field inside the onTimeSet method but I get a nullpointerexception. Then I thought I should initialize it in onCreateDialog but there I don't know how to use findViewById to find the actual text view. I'm sure this is probably simple but I have searched up and down without much luck. Thanks for your help!

推荐答案

您可以这样做:

1)解决方案1 ​​(更好):

public class MainActivity extends Activity {

        TextView resultText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            resultText = (TextView)findViewById(R.id./*YOUR TEXT VIEW ID*/);
        }

        //code from: http://developer.android.com/guide/topics/ui/controls/pickers.html
        public void showTimePicker(View v) {
            DialogFragment newFragment = new TimePickerFragment(resultText);
            newFragment.show(getFragmentManager(), "timePicker");
        }

        public class TimePickerFragment extends DialogFragment
                implements TimePickerDialog.OnTimeSetListener {

             TextView mResultText;

             public TimePickerFragment(TextView textView) {
                mResultText = textView;
            }

            @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) {
                String time = /*CONVERT YOUR TIME FROM hourOfDay and minute*/;
                mResultText.setText(time);
            }
        }
    }

2)解决方案2 :

public class MainActivity extends Activity {
    public TextView mResultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mResultText = (TextView)findViewById(R.id./*YOUR TEXT VIEW ID*/);
    }

    //code from: http://developer.android.com/guide/topics/ui/controls/pickers.html
    public void showTimePicker(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }

    public 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) {
            String time = /*CONVERT YOUR TIME FROM hourOfDay and minute*/;
            mResultText.setText(time);
        }
    }
}

这篇关于从TimePicker将TextView设置为时间-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆