0

在登录页面中,用户必须输入他的生日。我想创建一个 EditText(生日),然后 DatePicker 出现。

我喜欢这样:

public class Main extends Activity {

    EditText inputBirthDay;
    DatePicker dpResult;

    TextView tvDisplayDate;
    int year;
    int month;
    int day;

    static final int DATE_DIALOG_ID = 999;

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

        inputBirthDay = (EditText) findViewById(R.id.registerBirthDay);  
            inputBirthDay.setFocusableInTouchMode(false);       
        addListenerOnButton();      
    }   

    public void addListenerOnButton() {
        inputBirthDay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Calendar c = Calendar.getInstance();
                year = c.get(Calendar.YEAR);
                month = c.get(Calendar.MONTH);
                day = c.get(Calendar.DAY_OF_MONTH);
                showDialog(DATE_DIALOG_ID);
                                    inputBirthDay.requestFocus();   
            }
        });     
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

     inputBirthDay.setText(new StringBuilder().append(day)
                .append("-").append(month + 1).append("-                 ").append(year)
                .append(" "));
        }
    };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, datePickerListener, year, month,
                day);
        }
        return null;
    }
}

但我有一个问题:

1/当我点击EditText时,键盘第一次出现,然后当我再次点击时,DatePicker出现。如何隐藏键盘?

谢谢您的帮助

4

1 回答 1

1

对于第一名,这可能就足够了:

inputBirthDay.setInputType(InputType.TYPE_NULL);

对于 2 号,我认为:

inputBirthDay.setText(String.valueOf(year)+String.valueOf(month)+String.valueOf(day));

把它放在'onDateSet'函数中

于 2013-09-27T14:45:57.573 回答