Android:TimePickerDialog-TimePickerDialog.OnTimeSetListener [英] Android: TimePickerDialog - TimePickerDialog.OnTimeSetListener

查看:108
本文介绍了Android:TimePickerDialog-TimePickerDialog.OnTimeSetListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手.在这里,我再次提出另一个简单"的问题.我希望你能帮助我.我只想要一个在对话框中显示TimePicker的按钮.当我在时间选择器中设置时间时,我想获取选定的时间.我正在使用Android支持库和ActionbarSherlock.这是我的代码:

I am a newbie to Android. Here I am again with another "simple" question. I hope you can help me. I just want a button that displays a TimePicker in a dialog. When I set the time in the timepicker, I would like to get the selected time. I am using the Android support library and ActionbarSherlock. Here is my code:

AddPeriod.java

import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

public class AddPeriod extends SherlockFragmentActivity {


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

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

    TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(android.widget.TimePicker view,
                        int hourOfDay, int minute) {
                    Log.i("",""+hourOfDay+":"+minute);
                }
            };
}

add_period.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="Escoge hora" 
        android:onClick="showTimePickerDialog" />

</LinearLayout>

TimePickerFragment.java

import android.text.format.DateFormat;
import java.util.Calendar;

import android.app.Dialog;
import android.support.v4.app.DialogFragment;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.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) {
        // Do something with the time chosen by the user

    }
}

但是未调用侦听器.有什么建议?也许已经回答了,但是经过30分钟的搜索,我没有找到答案.非常感谢您的帮助.

But the listener is not being called. Any suggestions? Perhaps this has been answered but after 30 minutes of searching, I didn't find the answer.Thanks a lot for helping me.

推荐答案

在AddPeriod.java中,您创建了一个TimePickerDialog.OnTimeSetListener,但据我所知,没有使用该监听器.

In AddPeriod.java you create a TimePickerDialog.OnTimeSetListener but as far as I can see that listener isn't used.

在TimePickerFragment.java中,您实现了TimePickerDialog.OnTimeSetListener接口,并将片段实例本身作为侦听器传递给TimePickerDialog,但是您在TimePickerFragment.java中已实现的onTimeSet()方法中什么也不做.您是说没有调用TimePickerFragment.java中的onTimeSet()吗?你设置断点了吗?

In TimePickerFragment.java you implement the TimePickerDialog.OnTimeSetListener interface and pass the fragment instance itself as a listener to the TimePickerDialog but you do nothing in the implemented onTimeSet() method in TimePickerFragment.java. Are you saying that onTimeSet() in TimePickerFragment.java isn't called? Have you set a breakpoint?

如果您设法在片段中调用onTimeSet(),那么我假设您真正要问的是如何将此时间传递回活动.如果我没错,我会说这是使用事件总线的理想之地,例如,使用 Otto .

If you manage to get the onTimeSet() called in your fragment I assume what you're really asking for is how to pass this time back to the activity. If I'm correct I'd say that this is a perfect place to use an event bus, for instance using Otto.

更新:基于您对我的问题的评论,我认为您真正想要的是一种将TimePickerFragment中的选择时间返回给AddPeriod的方法.我认为更好的方法是使用事件总线(如上所述).这将使您的片段与活动脱钩.但是给您一个答案,它对您的代码所做的更改很少,我建议您将一个侦听器传递给该片段,并在创建对话框时使用该侦听器:

UPDATE: Based on your comments to my questions I assume that what you really want is a way to get the picked time in TimePickerFragment back to AddPeriod. The better approach is in my opinion to use an event bus (as mentioned above). This will decouple your fragment from your activity. BUT to give you an answer that works with minimal changes to your code I suggest that you pass a listener to the fragment and use that listener when you create your dialog:

TimePickerFragment.java:

TimePickerFragment.java:

public class TimePickerFragment extends DialogFragment {

    private TimePickerDialog.OnTimeSetListener listener;

    public TimePickerFragment(TimePickerDialog.OnTimeSetListener listener) {
        super();
        this.listener = listener;
    }

    @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(), listener, hour, minute,
        DateFormat.is24HourFormat(getActivity()));
    }
}

如您所见,我添加了一个带有TimePickerDialog.OnTimeSetListener参数的构造函数.创建TimePickerDialog时使用此侦听器.现在,在我们的AddPeriod活动中,将mTimeSetListener传递给创建时的片段:

As you can see I've added a constructor that takes a TimePickerDialog.OnTimeSetListener argument. This listener is used when the TimePickerDialog is created. Now in our AddPeriod activity we pass the mTimeSetListener to the fragment when it is created:

public class AddPeriod extends SherlockFragmentActivity {

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

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment(mTimeSetListener);
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

    TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(android.widget.TimePicker view,
                        int hourOfDay, int minute) {
                    Log.i("",""+hourOfDay+":"+minute);
                }
            };
}

这篇关于Android:TimePickerDialog-TimePickerDialog.OnTimeSetListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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