编程语言
首页 > 编程语言> > java – 停止用户多次单击edittext以打开多个对话框

java – 停止用户多次单击edittext以打开多个对话框

作者:互联网

我有一个非常简单的问题.我有一个EditText,当点击它时,打开一个DatePickerDialog.但是,反复单击EditText会打开几个对话框.是否有任何方法可以限制用户执行此操作,因此无论单击EditText多少次,都只显示一个对话框?

这是我的XML文件中的EditText:

<EditText
    android:id="@+id/etSetDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/spDifficultyLevel"
    android:drawableLeft="@drawable/ic_calendar_view"
    android:ems="10"
    android:focusableInTouchMode="false" 
    android:hint=" Set Date"
/>

这是代码:

/**
 * Where user selects date, launch datepicker on click
 */

public void selectDate() {
    etSetDate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //launch datepicker
            DialogFragment dialogFragment = new StartDatePicker();
            dialogFragment.show(getSupportFragmentManager(),
                "start_date_picker");
        }
    });
}

public class StartDatePicker extends DialogFragment implements
      DatePickerDialog.OnDateSetListener {
    Calendar c = Calendar.getInstance();
    int startYear = c.get(Calendar.YEAR);
    int startMonth = c.get(Calendar.MONTH);
    int startDay = c.get(Calendar.DAY_OF_MONTH);

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the date picker
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this,
            startYear, startMonth, startDay);
        return dialog;
    }
}

解决方法:

int flag = 0;
etSetDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //launch datepicker

            if(flag == 0)
            {
            DialogFragment dialogFragment = new StartDatePicker();
            dialogFragment.show(getSupportFragmentManager(),
                    "start_date_picker");
             flag = 1;
            }

        }

    });

当你关闭对话框时,再次设置flag = 0.

标签:java,android,android-edittext,onclick,android-datepicker
来源: https://codeday.me/bug/20190703/1365759.html