简体   繁体   English

从TimePicker将TextView设置为时间-Android

[英]Set TextView to time from TimePicker - Android

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. 我可以选择合适的时间,但无法选择要在我的TextView中显示的时间。 Java is not my preferred language so that is probably where I am falling down. Java不是我首选的语言,所以这可能是我无法接受的语言。

Here is my java code: 这是我的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!?
        }
    }
}

In my XML I have the Choose Time button run the showTimePicker method on click like so: 在我的XML中,我有“选择时间”按钮,在单击时运行showTimePicker方法,如下所示:

android:onClick="showTimePicker"

I have tried just setting the text field inside the onTimeSet method but I get a nullpointerexception. 我试过只在onTimeSet方法内设置文本字段,但得到一个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. 然后,我想应该在onCreateDialog上对其进行初始化,但是我不知道如何使用findViewById查找实际的文本视图。 I'm sure this is probably simple but I have searched up and down without much luck. 我敢肯定这可能很简单,但是我在运气不好的情况下上下搜索。 Thanks for your help! 谢谢你的帮助!

you can do it like that: 您可以这样做:

1) Solution 1 ( better ): 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) Solution 2 : 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);
        }
    }
}

You can actually use findViewById , as with getActivity() your fragment gets access to the activity it is running in and thus to the views inside the activity. 实际上,您可以使用findViewById ,就像使用getActivity()您的片段可以访问正在运行的活动,从而可以访问活动内部的视图。

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    TextView textView = (TextView) getActivity().findViewById(R.id.my_text_view);
    textView.setText(String.format("%02d", hourOfDay), String.format("%02d", minute);
}

This simple solution should be preferred over passing a reference to the TextView to the fragment, as this reference will be lost on reinstantiation of the fragment (see comment ). 与将对TextView的引用传递给片段相比,应该首选这种简单的解决方案,因为在重新实例化片段时,该引用将丢失(请参阅注释 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM