How to display year only in date picker in android

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

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

Thanks

6条回答
  •  天命终不由人
    2020-12-03 22:27

    I just have the same problem and all solution I found doesn't works with new Android Versions because I can't get the Field of the Reflection of DatePicker

    Anyway I solve by doing a Dialog to pick a number, if you need the code:

    SingleNumberPickerDialog.java Code

    public class SingleNumberPickerDialog extends DialogFragment {
    
        public interface IOnNumberSelected {
            void onNumberSelected(int num, int requestCode);
        }
    
        private static final String TAG = SingleNumberPickerDialog.class.getSimpleName();
        private static final String KEY = TAG + ".key";
        // Arguments Keys
        private static final String KEY_LABEL = KEY + ".label";
        private static final String KEY_START_VALUE = KEY + ".startValue";
        private static final String KEY_MIN_VALUE = KEY + ".minValue";
        private static final String KEY_MAX_VALUE = KEY + ".maxValue";
        private static final String KEY_REQUEST_CODE = KEY + ".requestCode";
        // Int Values
        private static final int INT_UNDEFINED = -0x1;
    
        @BindView(R2.id.tv_label)
        protected TextView mtvLabel;
        @BindView(R2.id.np_number)
        protected NumberPicker mnpNumber;
    
        private IOnNumberSelected mCallback;
        private int mRequestCode;
    
        public static SingleNumberPickerDialog newInstance(String label, int startValue, int minValue, int maxValue, int requestCode){
            SingleNumberPickerDialog mInstance = new SingleNumberPickerDialog();
            Bundle args = new Bundle();
            args.putString(KEY_LABEL, label);
            args.putInt(KEY_START_VALUE, startValue);
            args.putInt(KEY_MIN_VALUE, minValue);
            args.putInt(KEY_MAX_VALUE, maxValue);
            args.putInt(KEY_REQUEST_CODE, requestCode);
            mInstance.setArguments(args);
            return mInstance;
        }
    
        /** Override Lifecycle Methods **/
        @Override
        public void onAttach(@NonNull Context context) {
            super.onAttach(context);
            try {
                mCallback = (IOnNumberSelected) context;
            } catch (ClassCastException ccE){
                Log.e(TAG, ccE);
            }
        }
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            View view = inflater.inflate(R.layout.dialog_single_number_picker, container, false);
            ButterKnife.bind(this, view);
            initOnCreateView();
            return view;
        }
    
        /** Actions Methods **/
        @OnClick(R2.id.b_confirm)
        void onClickConfirm(View view){
            if(mCallback != null){
                mCallback.onNumberSelected(mnpNumber.getValue(), mRequestCode);
            }
        }
    
        /** Private Methods **/
        private void initOnCreateView(){
            if(getArguments() != null){
                initLabel();
                initStartValue();
                initMinValue();
                initMaxValue();
                initRequestCode();
            }
        }
    
        private void initLabel(){
            if(getArguments().containsKey(KEY_LABEL)){
                mtvLabel.setText(getArguments().getString(KEY_LABEL));
            }
        }
    
        private void initStartValue(){
            if(getArguments().containsKey(KEY_START_VALUE)){
                mnpNumber.setValue(getArguments().getInt(KEY_START_VALUE));
            }
        }
    
        private void initMinValue(){
            if(getArguments().containsKey(KEY_MIN_VALUE)){
                mnpNumber.setMinValue(getArguments().getInt(KEY_MIN_VALUE));
            }
        }
    
        private void initMaxValue(){
            if(getArguments().containsKey(KEY_MAX_VALUE)){
                mnpNumber.setMaxValue(getArguments().getInt(KEY_MAX_VALUE));
            }
        }
    
        private void initRequestCode(){
            if(getArguments().containsKey(KEY_REQUEST_CODE)){
                mRequestCode = getArguments().getInt(KEY_REQUEST_CODE);
            }
        }
    
    }
    

    dialog_single_number_picker.xml Code:

    
    
        
            
            
            

    Usage sample (u need to implement the callback inside ur activity):

    int year = DateUtils.getCurrentYear();
                SingleNumberPickerDialog mDialog = SingleNumberPickerDialog.newInstance(getString(R.string.label_year), year, year - 50, year + 50, 0x0);
                mDialog.setStyle(DialogFragment.STYLE_NO_FRAME, R.style.NoBackgroundDialogStyle);
                mDialog.setCancelable(false);
                mDialog.show(getSupportFragmentManager(), TAG_DIALOG_YEAR_PICKER);
    

提交回复
热议问题