从onTimeSet上的片段获取标记

时间:2018-02-22 12:10:46

标签: android android-dialogfragment android-timepicker

我有一个处理片段的MainActivity。在我做的其中一个片段上:

enterBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getActivity().getFragmentManager(), "timePickerExit");

        }
    });

    exitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getActivity().getFragmentManager(), "timePickerEnter");

        }
    });

在我的MainActivity上,我得到了结果:

public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener

功能:

@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {

//Who called it?
}

我如何知道在onTimeSet上调用它的是enterBtn还是exitBtn?

2 个答案:

答案 0 :(得分:1)

您可以为进入和退出按钮设置布尔标志。

//img is your black and white image of type BufferedImage

// Get the width and height of your image
int width = img.getWidth();
int height = img.getHeight();

// create a BitSet of the correct size      
BitSet bits = new BitSet(width * height);

// Iterate through your image's pixels and set the correct bits.
for (int y = 0; y < height; ++y)
{
    for (int x = 0; x < width; ++x)
    {
        // Check against 0xffffffff which is the RGB value of white.
        if (img.getRGB(x, y) == 0xffffffff)
        {
            bits.set(y * width + x);
        }
    }
}

和覆盖方法

isExit = false;
enterBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        isExit = false;
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getActivity().getFragmentManager(), "timePickerExit");

    }
});

exitBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        isExit = true;
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getActivity().getFragmentManager(), "timePickerEnter");

    }
});

答案 1 :(得分:1)

你可以做一些像

这样的事情
Thickness

然后在片段中点击Bundle bundle = new Bundle(); bundle.putInt("btnNumber", 1); //or 0 DialogFragment newFragment = new TimePickerFragment(); newFragment.setArguments(bundle); newFragment.show(getActivity().getFragmentManager(), "timePickerExit");

interface

然后private OnCompleteListener mListener; /// your otherDialogStuff public static interface OnCompleteListener { public abstract void onComplete(Int fragNumber, int hourOfDay, int minute); } // make sure the Activity implemented it @Override public void onAttach(Activity activity) { super.onAttach(activity); try { this.mListener = (OnCompleteListener)activity; } catch (final ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnCompleteListener"); } } 致电

onTimeSet

您的活动应实施 mListener.onComplete(0or1 ,12,12);