通过DialogFragment android将TimePickerDialog的结果设置为一个按钮

时间:2012-05-29 00:07:22

标签: android

美好的一天,我将是第一个承认,我根本没有使用片段的经验,只是学习所以请耐心等待我。正如标题所说,我试图将TimePickerDialog的结果设置为我活动中的按钮,但没有成功。当我单击开始按钮时,我看不到通常的TimePickerDialog,我看到的只是屏幕中间的一个小空对话框。那么请问我能做错什么?谢谢。

这是我的代码:

TimeDialog:

public class TimeDialog extends DialogFragment {
    private int mHour;
    private int mMinute;

    public TimeDialog(){

    }

    public static TimeDialog newInstance(int title){
        TimeDialog frag = new TimeDialog();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return null;

    }

    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

        @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;

                //updateDisplay();
            }

        /*private void updateDisplay() {

            View v = getActivity().getLayoutInflater().inflate(R.layout.add_resp, null);
            Button start = (Button)v.findViewById(R.id.start_time_button_id);
            start.setText(new StringBuilder().append(mHour).append(":").append(mMinute).toString());


        }*/
        };

    @Override
    public Dialog onCreateDialog(Bundle savedInstancestate){

        return new TimePickerDialog(getActivity(), mTimeSetListener, mHour, mMinute, false);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.add_resp, container, false);
        Button start = (Button)v.findViewById(R.id.start_time_button_id);
        start.setText(new StringBuilder().append(mHour).append(":").append(mMinute).toString());
        return v;

    }   
}

以下是我从活动中调用它的地方:

public class AddResp extends Activity implements OnClickListener {
    EditText text_title;
    Button start;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_resp);

        text_title = (EditText)findViewById(R.id.title_text_id);
        start = (Button)findViewById(R.id.start_time_button_id);

        start.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        int selection = view.getId();
        switch(selection){
        case R.id.start_time_button_id:

             DialogFragment newFragment = new DialogFragment();
                newFragment.show(getFragmentManager(), "dialog");
         break;       
        }
    }
}

1 个答案:

答案 0 :(得分:5)

要将数据从DialogFragment发送到Activity,您可以使用Handler类。

调用TimePicker对话框的主要活动:

    public class MainActivity extends Activity{ 

    Handler h = new Handler(){
        @Override
        public void handleMessage(Message msg){
            Button button = (Button) findViewById(R.id.btn);
            button.setText(msg.getData().getString("btn"));
        }
    };

    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.main);
    }

    public void btnClicked(View v){    
        TimepickerFragment dialog = new TimepickerFragment(h);
        dialog.show(getFragmentManager(), "timepickerdialog");
    }
}

TimePicker对话框:

    public class TimepickerFragment extends DialogFragment{
    Handler h;
    public TimepickerFragment(Handler arg_h){
        h = arg_h;      
    }       
    private TimePickerDialog.OnTimeSetListener callback = new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {         
            Message msg = new Message();
            Bundle data = new Bundle();
            data.putString("btn", "Selected Time - " + hourOfDay + ":" + minute);
            msg.setData(data);
            h.sendMessage(msg);             
        }
    };

    public Dialog onCreateDialog(Bundle bundle){
        int hourOfDay = 10;
        int minute = 10;
        boolean is24HourView = false;
        TimePickerDialog tpdialog = new TimePickerDialog(getActivity(), callback, hourOfDay, minute, is24HourView); 
        return tpdialog;
    }
}

注意:以上代码在API级别14中进行了测试