1

我正在使用时间选择器让用户输入他想要的时间来执行特定任务,我正在使用支持库中提供的 DialogFragment 类来向后兼容旧的 Android 版本。

这是我创建 TimePickerFragment 类的代码,在单独的文件中创建,取自: http: //developer.android.com/guide/topics/ui/controls/pickers.html

package com.calls.only;
import java.util.Calendar;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
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, false);
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}

主要活动:

package com.calls.only;


import java.util.Calendar;
import java.util.TimeZone;
import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

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

}

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
                    //Overriding onTimeSet causes an error, see below
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
               Log.i("TimePicker", "Time picker set!");
            }
        };

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

从日志中可以看出,未调用 onTimeset 方法,如果我尝试覆盖此方法,则会收到错误消息:“新 TimePickerDialog.OnTimeSetListener(){} 类型的方法 onTimeSet(TimePicker, int, int) 必须覆盖超类方法”

谁能告诉我问题是什么?我一直在试图弄清楚它让我感到非常沮丧!

4

2 回答 2

4

从日志中可以看出,未调用 onTimeset 方法

好吧,那是因为当您创建 时TimePickerDialog,您将片段提供为OnTimeSetListener

return new TimePickerDialog(getActivity(), this, hour, minute, false);
                                             ^

换句话说:您没有看到日志语句,因为mTimeSetListener您的活动中的变量永远不会被设置为您在片段中创建的对话框的侦听器。

您可以通过将片段附加到您的 Activity 来轻松解决此问题MainActivity,或者,如果您更喜欢可重用的东西,可以通过接口回调它。在这种情况下,您可以重用OnTimeSetListener接口,但您也可以设置自己的接口,例如,将Calendar对象传递回活动,而不是原始小时/分钟值。

在它的最基本形式中,它看起来有点像这样:

public class TimePickerFragment extends DialogFragment {

    @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
        // ... omitted

        if (!(getActivity() instanceof OnTimeSetListener)) throw new IllegalStateException("Activity should implement OnTimeSetListener!");
        OnTimeSetListener timeSetListener =  (OnTimeSetListener) getActivity();

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), timeSetListener, hour, minute, false);
    }
}

然后让你MainActivity实现相同的接口,而不是使用匿名内部类:

public class MainActivity extends FragmentActivity implements TimePickerDialog.OnTimeSetListener { 

    @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Log.i("TimePicker", "Time picker set!");
    }
}

更新:如评论中所述,要启用对多个选择器的支持,您有多种选择。一是跟踪托管活动中显示的对话框;如果您只需要区分两个选择器,布尔值就可以了,否则枚举将是实现两个以上状态的合适方法。您需要确保在配置更改期间保留这条信息......

但是,我更喜欢做的是能够onTimeSet(...)通过向每个选择器提供一个 id 来识别返回结果的来源。然后将该 id 包含在结果中,以便我们知道它来自哪里。我将在下面概述总体思路:

public class TimePickerFragment extends DialogFragment implements OnTimeSetListener {

    private int mId;
    private TimePickerDialogListener mListener;

    private static TimePickerFragment newInstance(int id) {
        Bundle args = new Bundle();
        args.putInt("picker_id", id);
        TimePickerFragment fragment = new TimePickerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
        // ... omitted

        mId = getArguments().getInt("picker_id");
        mListener = getActivity() instanceof TimePickerDialogListener ? (TimePickerDialogListener) getActivity() : null;

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute, false);
    }

    @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        if (mListener != null) mListener.onTimeSet(mId, view, hourOfDay, minute);
    }

    public static interface TimePickerDialogListener {
        public void onTimeSet(int id, TimePicker view, int hourOfDay, int minute);
    }

}

我们在上面所做的更改是将对话框本身注册为OnTimeSetListener,然后它将通过 传递数据TimePickerDialogListener,前提是托管活动实现了该接口。所以这就是我们下一步需要做的。另外,请注意,我添加了一个静态便捷方法来创建一个TimePickerFragment带有 id 的新方法。该值将设置为片段的参数,确保它成为片段状态的一部分,因此您不必担心自己的配置更改 - 框架会为您完成。

所以让我们改变MainActivity实现我们的自定义接口,并让它使用newInstance方法:

public class MainActivity extends FragmentActivity implements TimePickerFragment.TimePickerDialogListener { 

    private static final int START_TIME_PICKER_ID = 1;
    private static final int END_TIME_PICKER_ID = 2;

    public void InputStartTime(View v) {
        // supply the appropriate id - I'm assuming you'll be adding an InputEndTime method somewhere that will then supply END_TIME_PICKER_ID 
        DialogFragment newFragment = TimePickerFragment.newInstance(START_TIME_PICKER_ID);
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

    @Override public void onTimeSet(int id, TimePicker view, int hourOfDay, int minute) {
        Log.i("TimePicker", "Time picker set from id " + id + "!");

        // here you can compare the id value to figure out what picker this data came from
    }
}

最后一点:我是直接在浏览器中输入的,所以请注意任何明显的拼写错误。

于 2013-04-10T04:19:16.243 回答
1

我让代码使用它。

public class TimePickerFragment extends DialogFragment{
    @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(), (MainActivity)getActivity(), hour, minute, false);
    }
}


public class MainActivity extends FragmentActivity  implements TimePickerDialog.OnTimeSetListener{

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

    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        Log.i("TimePicker", "Time picker set!");
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
于 2013-04-10T04:24:03.630 回答