我是Android的新手。在这里,我再次提出另一个“简单”的问题。我希望你能帮助我。我只想要一个在对话框中显示TimePicker的按钮。当我在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分钟的搜索,我找不到答案。非常感谢你帮助我。
答案 0 :(得分: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);
}
};
}