DialogFragment创建多个对话框窗口而不是一个

时间:2016-06-01 08:47:25

标签: android android-fragments android-button android-dialog

我有一个标签式活动,其中包含两个底部标签。 FrameLayout填充了我创建的特殊布局文件。这两个布局文件由扩展Fragment类的java类运行。这是我现在所拥有的简要描述。

现在有关于问题的详细信息。布局文件包含两个EditText字段,单击时必须创建DatePickerDialog。我使用DialogFragment类实现了对话框,一个用于两个文本字段。 Hovewer,当我点击edittexts时,他们会生成几个DatePicker对话框,而不是一个,并且不清楚背后的逻辑是什么。它可能会产生2,3或甚至4个对话框窗口。

如何解决此问题?这是我的代码:

活动

public class Activity extends AppCompatActivity  {

    FragmentTabHost mFragmentTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_calculation);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mFragmentTabHost.setup(QuickCalculationActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);

        mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("days").setIndicator("DAYS"), DaysTabView.class, null);
        mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("date").setIndicator("DATE"), DateTabView.class, null);
    }
}

其中一个tabView类

public class DaysTabView extends Fragment {

/* 
 *    some code was not included to make easir to read 
 */

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.days_tab_view, container, false);
        mFromDate = (EditText) v.findViewById(R.id.fromDate);

        mFromDate.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mFromDate.setText("");
                SetDateToDefault(1);     //some method
                Bundle args = new Bundle();
                args.putInt("day", day1);
                args.putInt("month", month1);
                args.putInt("year", year1);
                myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
                d1.setArguments(args);
                d1.setOnDateSetListener(datePickerListener1);   //onDateSet Listener that is not included in this question
                d1.show(getFragmentManager(), "days");
                return false;
            }
        });
    }
}

和DialogFragment类

public class myDatePickerDialogFragment extends DialogFragment{

    int mDay, mMonth, mYear;
    OnDateSetListener onSetDate;

    public myDatePickerDialogFragment(){
    }

    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        mDay = args.getInt("day");
        mMonth = args.getInt("month");
        mYear = args.getInt("year");
    }

    public void setOnDateSetListener(OnDateSetListener setDate){
        onSetDate = setDate;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new DatePickerDialog(getActivity(), onSetDate, mYear, mMonth, mDay);
    }
}

4 个答案:

答案 0 :(得分:1)

在setOnTouchListener中返回true而不是false。

if(event.getAction() == MotionEvent.ACTION_UP){

            // Do what you want
            return true;
        }
        return false;
}

应该是这样的:

   mFromDate.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
             if(event.getAction() == MotionEvent.ACTION_UP){

                 mFromDate.setText("");
                SetDateToDefault(1);     //some method
                Bundle args = new Bundle();
                args.putInt("day", day1);
                args.putInt("month", month1);
                args.putInt("year", year1);
                myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
                d1.setArguments(args);
                d1.setOnDateSetListener(datePickerListener1);   //onDateSet Listener that is not included in this question
                d1.show(getFragmentManager(), "days");
                return true;
            }
            return false;
    }

        });

答案 1 :(得分:1)

我建议您在EditTexts上使用onClickListener更改DaysTabView。背后的原因是触发侦听器将在每次发生事件时触发(例如手指向下,手指移动,手指向上等),而点击侦听器只会被触发一次。

所以在mFromDate.setOnClickListener(new View.OnClickListener() { @Override public boolean onClick(View v) { mFromDate.setText(""); SetDateToDefault(1); //some method Bundle args = new Bundle(); args.putInt("day", day1); args.putInt("month", month1); args.putInt("year", year1); myDatePickerDialogFragment d1 = new myDatePickerDialogFragment(); d1.setArguments(args); d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question d1.show(getFragmentManager(), "days"); return false; } }); 课程中,改为:

{{1}}

答案 2 :(得分:1)

更改您的if(n&1) cout<<"Odd"; else cout<<"Even"; 代码

onTouchListener

答案 3 :(得分:1)

也许您应该使用ClickListener而不是TouchListener