gpt4 book ai didi

安卓:TimePickerDialog - TimePickerDialog.OnTimeSetListener

转载 作者:行者123 更新时间:2023-11-30 03:36:51 26 4
gpt4 key购买 nike

我是 Android 新手。在这里,我又提出了另一个“简单”的问题。我希望你可以帮助我。我只想要一个在对话框中显示 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 分钟的搜索,我没有找到答案。非常感谢您的帮助。

最佳答案

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

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

如果您设法在您的 fragment 中调用 onTimeSet(),我假设您真正要求的是如何将这段时间传递回 Activity 。如果我是对的,我会说这是使用事件总线的完美场所,例如使用 Otto .

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

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 Activity 中,我们在创建 fragment 时将 mTimeSetListener 传递给 fragment :

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);
}
};
}

关于安卓:TimePickerDialog - TimePickerDialog.OnTimeSetListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16541258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com