Custom date picker with months and years

后端 未结 1 1661
忘掉有多难
忘掉有多难 2020-12-18 14:49

How can I create a date picker for just months and years?

For example I want a picker with just

2012,2013 as year and 4,5,6,7 as

1条回答
  •  隐瞒了意图╮
    2020-12-18 15:29

    You can make your own like soo... I know this one has days as well but you get the point..

    Its composed of two views for each picker, representing buttons, with a ImageView in the center, and a textSwitcher that takes care of the numbers.

    enter image description here

    public class EditHomeworkActivity extends Activity implements ViewSwitcher.ViewFactory{
    
    private TextSwitcher mSwitcher1;
    private TextSwitcher mSwitcher2;
    private TextSwitcher mSwitcher3;
    
    public static int hwID;
    public static int change = 0;
    public static int year = 1819710;
    public static int day = 1819710;
    public static int month = 1819710;
    
    
    int prevMnth;
    
    Calendar cal;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    
        setContentView(R.layout.edit_homework_dialog);
    
        mSwitcher1 = (TextSwitcher) findViewById(R.id.day);
        mSwitcher1.setFactory(this);
    
        mSwitcher2 = (TextSwitcher) findViewById(R.id.month);
        mSwitcher2.setFactory(this);
    
        mSwitcher3 = (TextSwitcher) findViewById(R.id.year);
        mSwitcher3.setFactory(this);
    
        Animation in = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out);
    
        mSwitcher1.setInAnimation(in);
        mSwitcher1.setOutAnimation(out);
    
        mSwitcher2.setInAnimation(in);
        mSwitcher2.setOutAnimation(out);
    
        mSwitcher3.setInAnimation(in);
        mSwitcher3.setOutAnimation(out);
    
        Button confirm= (Button) findViewById(R.id.confirm);
        Button cancel = (Button) findViewById(R.id.cancel );
    
        cal = Calendar.getInstance();
    
        if(year != 1819710){cal.set(Calendar.YEAR, year);}
        if(month != 1819710){cal.set(Calendar.MONTH, month);}
        if(day != 1819710){cal.set(Calendar.DAY_OF_MONTH, day);}
    
        ImageButton dayP= (ImageButton) findViewById(R.id.dayP);
        ImageButton dayM= (ImageButton) findViewById(R.id.dayM);
    
        ImageButton monthP= (ImageButton) findViewById(R.id.monthP);
        ImageButton monthM= (ImageButton) findViewById(R.id.monthM);
    
        ImageButton yearP= (ImageButton) findViewById(R.id.yearP);
        ImageButton yearM= (ImageButton) findViewById(R.id.yearM);
    
    
        // add a click listener to the button
        confirm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    
                EditHomeworkActivity.DueYear = cal.get(Calendar.YEAR);
                EditHomeworkActivity.DueMonth = cal.get(Calendar.MONTH);
                EditHomeworkActivity.DueDay = cal.get(Calendar.DAY_OF_MONTH);
    
                finish();
            }
        });
    
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    
        dayP.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {cal.roll(Calendar.DAY_OF_YEAR, 1);update();}});
        dayM.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {cal.roll(Calendar.DAY_OF_YEAR, -1);update();}});
    
        monthP.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {cal.roll(Calendar.MONTH,  1);update();}});
        monthM.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {cal.roll(Calendar.MONTH, -1);update();}});
    
        yearP.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {cal.roll(Calendar.YEAR,  1);update();}});
        yearM.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {cal.roll(Calendar.YEAR, -1);update();}});
    
    
        update();
    }
    
    
    
    public void update(){
    
        mSwitcher1.setText(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
        mSwitcher2.setText(sort(cal.get(Calendar.MONTH)));
        mSwitcher3.setText(String.valueOf(cal.get(Calendar.YEAR)));
    
    
    }
    
    private CharSequence sort(int i) {
    
        String retrn = null;
    
        if(i == 0){retrn = "Jan";prevMnth = i;}
        if(i == 1){retrn = "Feb";}
        if(i == 2){retrn = "Mar";}
        if(i == 3){retrn = "Apr";}
        if(i == 4){retrn = "May";}
        if(i == 5){retrn = "Jun";}
        if(i == 6){retrn = "Jul";}
        if(i == 7){retrn = "Aug";}
        if(i == 8){retrn = "Sept";}
        if(i == 9){retrn = "Oct";}
        if(i == 10){retrn = "Nov";}
        if(i == 11){retrn = "Dec";prevMnth = i;}
    
        if(i == 12){if(prevMnth == 0){cal.roll(Calendar.MONTH, -1);}else{cal.roll(Calendar.MONTH, 1);}update();}
    
        return retrn;
    }
    
    public View makeView() {
        TextView t = new TextView(this);
        t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        t.setTextSize(36);
    
        return t;
    }
    
    
    
    
    }
    

    
        
    
            
    
            
    
            
    
            
    
            
    
                
    
                    
    
                    
    
                        
    
                        
    
                        
    
                        
    
                            
                            
    
                        
    
                        
    
                        
    
                        
                    
    
                    
    
                    
    
                        
    
                        
    
                        
    
                        
    
                            
                            
    
                        
    
                        
    
                        
    
                        
                    
    
                    
    
                    
    
                        
    
                        
    
                        
    
                        
    
                            
                            
    
                        
    
                        
    
                        
    
                        
                    
    
                    
                
    
            
    
            
    
            
    
                

    0 讨论(0)
提交回复
热议问题