我已经在片段(Work
)中的按钮按下时实现了时间选择器,但是如何仅使用一个位于?
这是我的MainActivity
中的我的onTimeSet()
:
public void onTimeSet(TimePicker timePicker, int i, int i1) {
TextView textView = (TextView)findViewById(R.id.textstart);
textView.setText("Hour: "+ i + " Minutes: "+ i1);
getSupportFragmentManager().getFragments();
}
这是我的
MainActivity
片段:Button timeButton = (Button) view.findViewById(R.id.timeButton);
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment timePicker = new TimePicker_Fragment();
timePicker.show(getFragmentManager(), "Time Picker");
}
});
Button timeButton2 = (Button) view.findViewById(R.id.timeButton2);
timeButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment timePicker2 = new TimePicker_Fragment();
timePicker2.show(getFragmentManager(), "Time Picker 2");
}
});
return view;
}
最佳答案
一种选择是更改应用程序的体系结构:
由于TimePickerFragment
是唯一可以理解TimePicker
中onTimeSet()
参数的参数,因此这是实现TimePickerDialog.OnTimeSetListener
的逻辑选择。TimePicker_Fragment
的两个实例可以通过一些自定义界面向托管报告WorkFragment
(并最终向Activity
)报告。使用一些唯一值(String
或枚举)在自定义版本的onTimeSet()
中标识它们。
例如,您的活动可以实现如下方法:
@Override
void onCustomTimeSet(String timePickerId, int hour, int minutes){
switch(timePickerId){
case "TIME_PICKER":
// code for time picker 1 here
break;
case "TIME_PICKER_2":
// code for time picker 1 here
break;
}
// ...
}
...由
onTimeSet(TimePicker, int, int)
中的“普通” TimePicker_Fragment
的实现调用@Override
void onTimeSet(TimePicker timePicker, int i, int i2){
((CustomTimePickerListener)getActivity()).onCustomTimeSet("TIME_PICKER_1", i, i2)
}
另一个选择是利用
TimePicker
是View
的事实,并且View
可以具有标签(可以是任何类型的Object
,请参见View.setTag())。因此,您可以将
TimePicker_Fragment
设置为TimePicker
标记。由于
TimePicker
是TimePickerDialog
的一部分,因此设置标签的最简单方法似乎是(再次)实现TimePicker_Fragment
。但是这次,TimePickerDialog.OnTimeSetListener
可以实现相同的接口:Activity
中的代码void onTimeSet(TimePicker timePicker, int i, int i2){
timePicker.setTag("TIME_PICKER");
((TimePickerDialog.OnTimeSetListener)getActivity()).onTimeSet(timePicker, i, i2);
}
TimePicker_Fragment
中的代码void onTimeSet(TimePicker timePicker, int i, int i2){
String tag = (String)timePicker.getTag();
switch(tag){
case "TIME_PICKER":
// code for time picker 1 here
break;
case "TIME_PICKER_2":
// code for time picker 2 here
break;
}
// ...
}
为三个相关组件实现第二个解决方案的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new Work()).commit();
}
public void onTimeSet(TimePicker timePicker, int i, int i1){
String tag = (String)timePicker.getTag();
switch(tag){
case TIME_PICKER:
TextView textView = (TextView)findViewById(R.id.textstart);
textView.setText("Start Hour: "+ i + " Minutes: "+ i1);
break;
case TIME_PICKER_2:
TextView textView2 = (TextView)findViewById(R.id.textend);
textView2.setText("Start Hour: "+ i + " Minutes: "+ i1);
break;
}
}
}
工作库
public class Work extends Fragment {
public static final String TIME_PICKER = "TIME_PICKER";
public static final String TIME_PICKER_2 = "TIME_PICKER_2";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_work, container, false);
getActivity().setTitle("Work");
Button timeButton = (Button) view.findViewById(R.id.btn1);
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment timePicker = TimePicker_Fragment.instance(TIME_PICKER);
timePicker.show(getFragmentManager(), "Time Picker");
}
});
Button timeButton2 = (Button) view.findViewById(R.id.btn2);
timeButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment timePicker2 =TimePicker_Fragment.instance(TIME_PICKER_2);
timePicker2.show(getFragmentManager(), "Time Picker 2");
}
});
return view;
}
}
TimePicker_Fragment.java
public class TimePicker_Fragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
private static final String KEY_TIMEPICKER_TAG = "KEY_TIMEPICKER_TAG";
/**
*
* @param timePickerTag the tag by which you can identify a TimePicker.
* Will be set as tag to the TimePicker View in onTimeSet()
* @return x
*/
public static TimePicker_Fragment instance(String timePickerTag){
TimePicker_Fragment fragment = new TimePicker_Fragment();
Bundle b = new Bundle();
b.putString(KEY_TIMEPICKER_TAG, timePickerTag);
fragment.setArguments(b);
return fragment;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
Context ctx = getContext();
return new TimePickerDialog(ctx, this, hour, minute, DateFormat.is24HourFormat(ctx));
}
public void onTimeSet(TimePicker timePicker, int i, int i2){
Bundle b = getArguments();
assert b != null;
timePicker.setTag(b.getString(KEY_TIMEPICKER_TAG));
((TimePickerDialog.OnTimeSetListener)getActivity()).onTimeSet(timePicker, i, i2);
}
}
关于java - 如何在同一onTimeSet()中提取两个时间选择器对话框的小时和分钟?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56876162/