我目前正在尝试实现Android BetterPickers库(https://github.com/derekbrameyer/android-betterpickers)。我已经能够显示要求用户选择时间的选择器,但是没有调用该界面提供的方法。方法是https://github.com/derekbrameyer/android-betterpickers/blob/master/sample/src/com/doomonafireball/betterpickers/sample/activity/hmspicker/SampleHmsDefault.java处的第43行。

这是我的代码的样子。我的代码与提供的示例之间的主要区别在于,我的代码是在Fragment中执行的,而不是在Activity中执行(github示例中的第35行)。我使用了getActivity()。getSupportFragmentManager()而不是getSupportFragmentManager()。为什么不调用onDialogHmsSet方法?

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.doomonafireball.betterpickers.hmspicker.HmsPickerBuilder;
import com.doomonafireball.betterpickers.hmspicker.HmsPickerDialogFragment.HmsPickerDialogHandler;
import com.example.hiittimer.R;

public class NewWorkout extends Fragment implements OnClickListener, HmsPickerDialogHandler {
    private View view;
    private Button btnSetOn, btnSetOff;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_activity_new_workout, container, false);
    viewHolder = new ViewHolder();
    setViews();
    return view;
}

private void setViews(){
    btnSetOff = (Button) view.findViewById(R.id.btn_set_on);
    btnSetOn = (Button) view.findViewById(R.id.btn_set_off);
    btnSetOff.setOnClickListener(this);
    btnSetOn.setOnClickListener(this);



}


@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_set_on:
        HmsPickerBuilder hpb = new HmsPickerBuilder().
        setStyleResId(R.style.BetterPickersDialogFragment)
                    .setFragmentManager(getActivity().getSupportFragmentManager();
        hpb.show();


        break;

    case R.id.btn_set_off:
        break;

    default:
        break;
    }
}

@Override
public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) {
    Assert.assertTrue(false);
}


}

最佳答案

我想到了。解决方案在https://github.com/derekbrameyer/android-betterpickers/blob/master/sample/src/com/doomonafireball/betterpickers/sample/fragment/SampleTimeFragment.java

08-04 01:01