如何从时间戳中将时间传递给textview,它们位于制表符内并扩展了片段

时间:2017-06-13 08:09:09

标签: android android-fragments timepicker

我是android的新手,我试图通过点击按钮(这里我使用标签布局)将时间选择器的结果传递给textview,问题是我没有得到任何输出,我不知道是否是工作与否。 有谁能帮我解决这个问题?

 public class Tabwhen extends Fragment {
        @Nullable
        @Override

        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.when, container, false);

            final TextView time;
            final TimePicker timePicker;

            time = (TextView) view.findViewById(R.id.set_time1);
            timePicker = (TimePicker) view.findViewById(R.id.timePicker);
            Button set_time = (Button) view.findViewById(R.id.btn_set_time);

            set_time.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                        @Override
                        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                            // display a toast with changed values of time picker
                            Toast.makeText(getActivity().getApplicationContext(), hourOfDay + " : " + minute, Toast.LENGTH_SHORT).show();

                            time.setText(hourOfDay +":"+ minute); // set the current time in text view
                        }
                    });


                }





       });

            return view;

        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个

所以定义一个私有字符串,如mSelectedTime;

final TextView time = (TextView) mView.findViewById(R.id.tv);
TimePicker timePicker = (TimePicker) mView.findViewById(R.id.dp);

timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        mSelectedTime = hourOfDay + " " + ":" + " " +minute;
    }
});

然后在你的听众中:

set_time.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         time.setText(mSelectedTime)
}