2

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

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 分钟的搜索,我没有找到答案。非常感谢您帮助我。

4

1 回答 1

1

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

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

如果您设法在片段中调用 onTimeSet(),我假设您真正要求的是如何将这段时间传递回活动。如果我是正确的,我会说这是使用事件总线的理想场所,例如使用Otto

更新:根据您对我的问题的评论,我假设您真正想要的是一种将 TimePickerFragment 中选择的时间返回到 AddPeriod 的方法。在我看来,更好的方法是使用事件总线(如上所述)。这将使您的片段与您的活动分离。但是为了给您一个对代码更改最少的答案,我建议您将侦听器传递给片段并在创建对话框时使用该侦听器:

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 传递给片段:

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);
                }
            };
}
于 2013-05-14T11:01:23.413 回答