时间选择器片段分化

Time Picker Fragments Differentiation

我 运行 遇到一个问题,我的应用程序正在正确构建并且按钮按预期工作,但我正在使用两个时间选择器片段编辑的文本字段正在由开始时间选择器和结束时间选择器。

public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button_start = (Button) findViewById(R.id.start_picker_button);
        button_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment timePicker1 = new TimePickerStartFragment();
                timePicker1.show(getSupportFragmentManager(),"time picker start");
            }
        });

        Button button_end = (Button) findViewById(R.id.end_picker_button);
        button_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment timePicker2 = new TimePickerEndFragment();
                timePicker2.show(getSupportFragmentManager(),"time picker end");
            }
        });

    }

    @Override
    public void onTimeSet(TimePicker TimePickerStartFragment, int hourOfDay, int minute) {
        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText("Hour: " + hourOfDay + " " + "Minute: " + minute);

我的应用程序看起来像两个按钮,一个用于开始时间片段,另一个用于结束时间片段。我有两个单独的时间选择器片段 java 文件。但是,当我设置结束时间片段时,它仍会修改开始时间文本。我想让开始时间选择器只修改开始时间文本,结束时间选择器修改结束时间文本。

为了进一步阐明何时按下 select 开始时间按钮 TimePickerStratFragment 被打开并且时间被 selected,这修改了一个名为 textView 的文本字段。但是,当时间结束按钮片段被 selected 时,它也会修改文本视图。我希望时间结束按钮不修改文本视图。

添加一个字符串值来检查哪个时间选择器被打开,因为 onTimeSet 被触发用于两个片段使用类似这样的东西

String timeTag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button_start = (Button) findViewById(R.id.start_picker_button);
        button_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeTag = "from";
                DialogFragment timePicker1 = new TimePickerStartFragment();
                timePicker1.show(getSupportFragmentManager(),"time picker start");
            }
        });

        Button button_end = (Button) findViewById(R.id.end_picker_button);
        button_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeTag = "to";
                DialogFragment timePicker2 = new TimePickerEndFragment();
                timePicker2.show(getSupportFragmentManager(),"time picker end");
            }
        });

    }

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

        if(Objects.equals(timeTag, "from")) {
            TextView textView = (TextView)findViewById(R.id.textView);
            textView.setText("Hour: " + hourOfDay + " " + "Minute: " + minute);
        }

        if(Objects.equals(timeTag, "to")) {

        }
}