多个TimePickers Android

时间:2018-08-03 12:34:47

标签: java android

我正在尝试使用时间选择器在一页上设置多次,但无法弄清楚该如何做。到目前为止,我可以将一个文本框设置一次。 TextViewA

我想做的是设置Textview谁的onClick事件触发了时间选择器的文本。

示例当我按下Time1时,它将调用OnClick并将Time1.Text设置为我所分配的时间,并且与Time2相同。

时间选择器类:

public class TimePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    return new TimePickerDialog(getActivity(),(TimePickerDialog.OnTimeSetListener)getActivity(),hour,minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}

OnClick调用Timepicker:

Time1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
        DialogFragment timePicker = new TimePickerFragment();
        timePicker.show(getSupportFragmentManager(),"time picker");

        }
    });

TimePicker Methood:

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

    TextView Time1=  (TextView)findViewById(R.id.TextViewA);
    Tim1.setText(  hourOfDay + ":" +minute);

}

任何帮助都会得到感谢!

1 个答案:

答案 0 :(得分:0)

显示时间选择器时设置标签。并检查回调。

int which;

Time1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
        which = 0;        
        ...
        }
    });

Time1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            which = 1;        
            ...
            }
        });

并查看何时获得回调。

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    if(which == 0) {
      Time1.setText(  hourOfDay + ":" +minute); 
    }
    else if (which == 1) {
      Time2.setText(  hourOfDay + ":" +minute); 
    }
}