如何通过DatePickerDialog中的更改数据设置焦点?

时间:2017-11-23 10:15:44

标签: android

首先,我很抱歉我不擅长英语, 但我会尽力写出来。

从DatePickerDialog中选择日期并再次选择日期时,

第一次选择时,

焦点拾取器不会移动到更改日期。

我该怎么做才能解决这个问题?

AddProjectOrEvent.java

@Override
public void onClick(View view) {
    Log.d("AddProjectOrEvent", "onClick() execute!");

    if (view == TV_ADDpe_EvDate_startDate) {
        showDatePickerDialog(AddProjectOrEvent.this, TV_EV_DATE_START_DATE, dateSetListener, year, month, day);
    }





private void showDatePickerDialog(Context context, int FLAG, DatePickerDialog.OnDateSetListener dateSetListener, int year, int month, int day) {
    Log.d("AddProjectOrEvent", "showDatePickerDialog() execute!");

    Flag_What_DateSet = FLAG;
    new DatePickerDialog(context, dateSetListener, year, month, day).show();
}





DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        Log.d("AddProjectOrEvent", "dateSetListener_onDateSet() execute!");

        if (Flag_What_DateSet == TV_EV_DATE_START_DATE) {
            setText(TV_EV_DATE_START_DATE, TV_ADDpe_EvDate_startDate, year, month, day);
        } 
    }
};

这是我正在谈论的问题的代码。

这太复杂了,所以我减少了给你看。

Picker已被此代码所暗示,

我不知道该实施什么。

4 个答案:

答案 0 :(得分:1)

试试这个......

此编码会为第一次点击设置日期选择器当前日期,为第二次点击 选择日期 强>

public class MainActivity extends AppCompatActivity {

    private int year;
    private int month;
    private int day;
    private EditText signup_bday;

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

        signup_bday=(EditText)findViewById(R.id.edittext_signup_birthday);

        signup_bday.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDatePickerDialog(signup_bday.getText().toString());
            }
        });
    }

    private void showDatePickerDialog(String date) {
        if (date.contentEquals("")) {
            Calendar mcurrentDate=Calendar.getInstance();
            year=mcurrentDate.get(Calendar.YEAR);
            month=mcurrentDate.get(Calendar.MONTH);
            day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
        }else{
            String[] split = date.split("-");
            day = Integer.valueOf(split[0]);
            int montx = Integer.valueOf(split[1]);
            month = montx -1;
            year = Integer.valueOf(split[2]);
        }

        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int curyear, int monthOfYear,
                                  int dayOfMonth) {
                // TODO Auto-generated method stub
                signup_bday.setText(new StringBuilder().append(dayOfMonth).append("-").append(monthOfYear + 1).append("-").append(curyear));
            }
        };

        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                dateSetListener, year, month, day);
        datePickerDialog.show();
    }
}

答案 1 :(得分:0)

我真的不了解您的代码,但是从您编写的内容来看,您似乎正在尝试为项目添加日期选择器。在这里,我将提供一个您将在Activity中调用的DatePicker。

创建一个名为DatePickerFrag的片段,并将以下代码粘贴到framgent中:

public class DatePickerFrag extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    AdapterViews.OnDateListener mCallback;
    OnDateChangeListener mCall;

    public interface OnDateListener{
        void onDate(int month, int year, int date);
    }

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        mCallback = (OnDateListener)context;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedState){
        Calendar mCalendar = Calendar.getInstance();
        int month = mCalendar.get(Calendar.MONTH);
        int year = mCalendar.get(Calendar.YEAR);
        int day = mCalendar.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        mCallback.onDate(month, year, day);
    }
}

然后,单击按钮时显示日期选择器:

@Override
public void onClick(View view) {
    Log.d("AddProjectOrEvent", "onClick() execute!");

    if (view == TV_ADDpe_EvDate_startDate) {
            DatePickerFrag dialog = new DatePickerFrag();
            dialog.show(getSupportFragmentManager(), "datepicker");
    }

现在通过在您的活动中实现DatePickerFrag的界面来获取日期值。

例如:

public class MyActivity extends Activity implements OnDateChangeListener{

    @Override
    public void onDate(int month, int year, int day) {
        //Get the set month, year and day here in Integers

    }...}

就是这样。

答案 2 :(得分:0)

请从下面的代码中查看您的代码。它会工作。

public class DateDialogFragment扩展了DialogFragment实现 DatePickerDialog.OnDateSetListener {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
    dialog.getDatePicker().setMinDate(System.currentTimeMillis());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    return  dialog;
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(year, month, dayOfMonth);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    String formattedDate = sdf.format(c.getTime());

}

}

答案 3 :(得分:0)

首先,您需要对日历进行全局声明。

Calendar calendar;

其次,在Activity的onCreate()中实例化它。

calendar = Calendar.getInstance();

现在,对于EditText的onClick,您可以拥有这样的代码..

et_calendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int YEAR = calendar.get(Calendar.YEAR);
                int MONTH=calendar.get(Calendar.MONTH);
                int DAY=calendar.get(Calendar.DAY_OF_WEEK);
                DatePickerDialog datePickerDialog = new DatePickerDialog(DummyDialog.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                         calendar.set(year,month,dayOfMonth);
                         et_calendar.setText(""+dayOfMonth+"-"+(month+1)+"-"+year);
                    }
                },YEAR,MONTH,DAY);
            }
        });

第一次,你的对话框打开了系统日期..当你点击它上面的某个日期时,那个日期将被分配给全局日历实例..所以,从你第二次在病房开始选中将会集中精力。