13

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

我已经看到了可以使用setOnClickListener或的示例setOnTouchListener,哪一个是最好的?

我也看到了几个不同的设计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);
    }


}
4

3 回答 3

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();  }
    });

试试这个……评论你的想法……谢谢……

于 2013-07-23T12:14:44.983 回答
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();

            }
        });
于 2016-12-18T07:38:08.010 回答
0

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

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

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

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);
}

在您的活动集中,您可以根据需要编辑文本值:

private EditText editText; 
//...

附加 onclick 监听器:

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

        }
    });
于 2016-12-23T19:07:42.070 回答