How to display year only in date picker in android

前端 未结 6 1289
一整个雨季
一整个雨季 2020-12-03 21:59

Can anybody tell how to display year only in date picker in android

Thanks

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 22:16

    private void showBirthDayPicker() {
        int year = calendar.get(Calendar.YEAR);
    
        final Dialog birthDayDialog = new Dialog(this);
        birthDayDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        birthDayDialog.setContentView(R.layout.dialog_birthday);
    
        Button okBtn = birthDayDialog.findViewById(R.id.btn_ok);
        Button cancelBtn = birthDayDialog.findViewById(R.id.btn_cancel);
    
        final NumberPicker np = birthDayDialog.findViewById(R.id.birthdayPicker);
        np.setMinValue(year - 100);
        np.setMaxValue(year - 15);
        np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        setDivider(np, android.R.color.white);
        np.setWrapSelectorWheel(false);
        np.setValue(year-20);
        np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
    
            }
        });
    
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edtBirthDay.setText(String.valueOf(np.getValue()));
                birthDayDialog.dismiss();
            }
        });
    
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                birthDayDialog.dismiss();
            }
        });
    
        birthDayDialog.show();
    }
    

    xml

       `enter code here`
       
    
        
    
            
    
            
    
            
    
                

提交回复
热议问题