android的简单datepicker

时间:2013-12-26 09:40:55

标签: java android datepicker

我试图点击编辑文本

启动DataPicker

我想做什么 ::

  1. 有两个编辑文本
  2. 我想从两个编辑文本
  3. 启动datepicker

    我做了什么 ::

    1. 我试图这样做

    2. 我已将其中一个edittext作为样本

    3. 问题 ::

        点击

        时,
      1. 日期选择器未启动

      2. logcat中没有错误

      3. MainActivity.java

        public class MainActivity extends FragmentActivity {
        
            EditText edt;
            EditText edt2;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                edt=(EditText)findViewById(R.id.editText1);
                edt2=(EditText)findViewById(R.id.editText2);   
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                Calendar c = Calendar.getInstance();
                Calendar c1=Calendar.getInstance();
                c1.setTime(new Date());
                c.setTime(new Date()); // Now use today date.
                c.add(Calendar.DATE, 7); // Adding 7 days
                String ToDate = sdf.format(c.getTime());
                String FromDate = sdf.format(c1.getTime());
                edt2.setText(FromDate);
                edt.setText(ToDate);
            }
        
        
        
             public void selectDate(View view) {
                    DialogFragment newFragment = new SelectDateFragment();
                    newFragment.show(getSupportFragmentManager(), "DatePicker");
                }
                public void populateSetDate(int year, int month, int day) {
                    edt2 = (EditText)findViewById(R.id.editText1);
                    edt2.setText(day+"/"+month+"/"+year);
                }
                public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
                    @Override
                    public Dialog onCreateDialog(Bundle savedInstanceState) {
                        final Calendar calendar = Calendar.getInstance();
                        int yy = calendar.get(Calendar.YEAR);
                        int mm = calendar.get(Calendar.MONTH);
                        int dd = calendar.get(Calendar.DAY_OF_MONTH);
                        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
                    }
        
                    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
                        populateSetDate(yy, dd, mm+1);
                    }
                }   
        }
        

3 个答案:

答案 0 :(得分:2)

您只需在ClickClick上实现Edittext onClick()事件:

edt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
               // new DatePickerDialog(ProximityActivity.this, dateD, myCalendar
                        //.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        //myCalendar.get(Calendar.DAY_OF_MONTH)).show();

                      //Load your DiaglogFragment Over here
            }
        });

答案 1 :(得分:1)

您是否忘记在onCreate中设置onClickListener?或者它是否在xml中处理?

edt2.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        selectDate(null);//I set null cause I don't know in the function what should the view be.
    }
});

答案 2 :(得分:1)

由于您未在代码中为onClickListener设置EditText,因此未启动DatePicker。

为EditText设置点击监听器,如下所示。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edt=(EditText)findViewById(R.id.editText1);
    edt2=(EditText)findViewById(R.id.editText2);   
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Calendar c = Calendar.getInstance();
    Calendar c1=Calendar.getInstance();
    c1.setTime(new Date());
    c.setTime(new Date()); // Now use today date.
    c.add(Calendar.DATE, 7); // Adding 7 days
    String ToDate = sdf.format(c.getTime());
    String FromDate = sdf.format(c1.getTime());
    edt2.setText(FromDate);
    edt.setText(ToDate);

    edt2.setOnClickListener(new OnClickListener() {
       @Override
      public void onClick(View v) {
       selectDate(v);
        }
   });

  edt.setOnClickListener(new OnClickListener() {
       @Override
      public void onClick(View v) {
       selectDate(v); //Pass the View as parameter. 
        }
   });
}