Android BetterPickers

时间:2013-08-25 21:17:57

标签: android interface android-fragments

我目前正在尝试实施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行。

这是我的代码的样子。我的代码和提供的示例之间的主要区别在于我的代码是在片段中执行而不是在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);
}

}

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

在fragmentmanager中添加.addNumberPickerDialogHandler(yourclassname.this) 检查下面的代码...它对我有用

FragmentManager fragManager = getFragmentManager();
NumberPickerBuilder npb = new NumberPickerBuilder()
    .addNumberPickerDialogHandler(CashTransaction.this)
    .setFragmentManager(fragManager)
    .setStyleResId(R.style.BetterPickersDialogFragment_Light);

  npb.show();

enter code here

答案 2 :(得分:0)

在致电NumberPickerBuilder时,您必须设置setTargetFragment

  

thisfragment.this - >目前的片段。

  NumberPickerBuilder npb = new NumberPickerBuilder()
                        .setFragmentManager(activity.getSupportFragmentManager())
                        .setStyleResId(R.style.BetterPickersDialogFragment)
                        .setTargetFragment(this) // or .setTargetFragment(fragment.this)
                        .setLabelText(getString(R.string.day_c));