EditText的弹出DatePicker

时间:2013-07-23 11:04:06

标签: android android-datepicker

一直在尝试查看关于DatePickers弹出EditText的其他问题,但我在使其工作时遇到了问题。

我见过哪些例子可以使用setOnClickListenersetOnTouchListener,哪一个最好?

另外我见过DatePicker的几个不同的设计,你如何改变设计?

到目前为止,下面是我的代码,尝试使用其他示例中的代码但无法使其正常工作。

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;


/**
 * Created by MOS182 on 7/21/13.
 */
public class AddReminder extends Activity {

    TextView Title, Amount, PaymentDate, ReminderDate, ReminderTime;
    EditText eTitle, eAmount, ePaymentDate, eReminderDate, eReminderTime;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminders_dialog);
        initializeVariables();
    }



    private void initializeVariables()
    {
        Title = (TextView) findViewById(R.id.tvTitle);
        Amount = (TextView) findViewById(R.id.tvAmount);
        PaymentDate = (TextView) findViewById(R.id.tvPaymentDate);
        ReminderDate = (TextView) findViewById(R.id.tvReminderDate);
        ReminderTime = (TextView) findViewById(R.id.tvReminderTime);
        eTitle = (EditText) findViewById(R.id.etTitle);
        eAmount = (EditText) findViewById(R.id.etAmount);
        ePaymentDate = (EditText) findViewById(R.id.etPaymentDate);
        eReminderDate = (EditText) findViewById(R.id.etReminderDate);
        eReminderTime = (EditText) findViewById(R.id.etReminderTime);
    }


}

3 个答案:

答案 0 :(得分:50)

我使用 setOnClickListener EditText方法来显示DatePicker

然后在xml文件中设置EditText属性android:focusable="false"。这样就可以避免使用焦点和虚拟键盘。

yourEditText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //To show current date in the datepicker
            Calendar mcurrentDate=Calendar.getInstance();
            mYear=mcurrentDate.get(Calendar.YEAR);
            mMonth=mcurrentDate.get(Calendar.MONTH);
            mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker=new DatePickerDialog(**YourActivityName**.this, new OnDateSetListener() {                  
                public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                    // TODO Auto-generated method stub                      
                    /*      Your code   to get date and time    */
                }
            },mYear, mMonth, mDay);
            mDatePicker.setTitle("Select date");                
            mDatePicker.show();  }
    });

尝试这个...评论你的想法...谢谢..

答案 1 :(得分:1)

/* create calndar object */  
  Calendar myCalendar = Calendar.getInstance();
/* find textview*/
date = (TextView) findViewById(R.id.stdate);
/* and copy the fallowing code*/
final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
              @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "dd/MMM/yyyy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.UK);

                date.setText(sdf.format(myCalendar.getTime()));

            }

        };


        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(MainActivity.this, datePickerListener, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();

            }
        });

答案 2 :(得分:0)

对于较旧的API,即我仍然尝试支持16 ...所以:

来自http://javapapers.com/android/android-datepicker/

您可以使用实现DatePickerDialog的DialogFragment作为exampled:

public static class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    // Thanks to -> http://javapapers.com/android/android-datepicker/
    EditText mEditText;
    public SelectDateFragment(EditText mEditText){
        this.mEditText = mEditText;
    }

    @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(mEditText, yy, mm+1, dd);
    }
}

public void selectDate() {
    DialogFragment newFragment = new SelectDateFragment(editText);
    newFragment.show(getFragmentManager(), "DatePicker");
}
public static void populateSetDate(EditText mEditText, int year, int month, int day) {
    mEditText.setText(month+"/"+day+"/"+year);
}

在您的活动中,根据需要设置editText值:

private EditText editText; 
//...

附加onclick侦听器:

    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectDate();

        }
    });