我已经在按钮按下时实现了时间选择器,这些时间选择器位于一个片段(Work
)中,但是我如何仅使用一个onTimeSet()来提取两个对话框的小时和分钟来分隔变量。位于MainActivity
中?
这是我的onTimeSet()
中的我的MainActivity
:
public void onTimeSet(TimePicker timePicker, int i, int i1) {
TextView textView = (TextView)findViewById(R.id.textstart);
textView.setText("Hour: "+ i + " Minutes: "+ i1);
getSupportFragmentManager().getFragments();
}
这是我的Work
片段:
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;
}
答案 0 :(得分:1)
一种选择是更改应用程序的体系结构:
由于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
可以具有 tag (可以是任何Object
的类型,请参见View.setTag())。
因此,您可以TimePicker_Fragment
为TimePicker
设置标签。
由于TimePicker
是TimePickerDialog
的一部分,所以设置 tag 的最简单方法似乎是(再次)TimePicker_Fragment
实现{{ 1}}。但是这次,TimePickerDialog.OnTimeSetListener
可以实现相同的接口:
Activity
中的代码
TimePicker_Fragment
void onTimeSet(TimePicker timePicker, int i, int i2){
timePicker.setTag("TIME_PICKER");
((TimePickerDialog.OnTimeSetListener)getActivity()).onTimeSet(timePicker, i, i2);
}
中的代码
Activity
MainActivity.java
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;
}
// ...
}
Work.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;
}
}
}
TimePicker_Fragment.java
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;
}
}
答案 1 :(得分:0)
我不确定我是否理解这个问题。
您是否尝试过类似的方法(不确定确切的语法):
public void onTimeSet(TimePicker timePicker, int i, int i1) {
TextView textView = (TextView)findViewById(R.id.textstart);
textView.setText("Hour: "+ timePicker.getHour() + " Minutes: "+ timePicker.getMinute());
}
您还可以将监听器添加到TimePicker_Fragment
构造函数中,以获取新的小时和分钟。