android 分别显示年控件、年月控件、年月日控件

最近项目需要单独显示选择年份、选择年月和选中年月日的控件需求,就自己研究了一下,顺便做记录。话不多说,进入正题。。。

先看一下效果图:

只显示年的控件
只显示年份的控件

首先写一个activity页面:

public class DateActivity extends Activity {

    private EditText etYear, etMonth, etDay;
    private String month;

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

        //年
        etYear = findViewById(R.id.et_year);
        etYear.setText(new SimpleDateFormat("yyyy").format(new Date()));
        findViewById(R.id.iv_year).setOnClickListener(new OnPickYearClickListener(this, etYear));

        //年月
        etMonth = findViewById(R.id.et_month);
        etMonth.setText(new SimpleDateFormat("yyyy-MM").format(new Date()));
        findViewById(R.id.iv_month).setOnClickListener(new OnPickMonthClickListener(this, etMonth));
        

        //年月日
        etDay = findViewById(R.id.et_day);
        etDay.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        findViewById(R.id.iv_day).setOnClickListener(new OnPickDateClickListener(this, etDay));
    }

}

activity对应的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_height"
            android:gravity="center"
            android:text="年份:" />

        <EditText
            android:id="@+id/et_year"
            android:layout_width="@dimen/sp_width"
            android:layout_height="@dimen/btn_height"
            android:enabled="false"
            android:gravity="center"
            android:textColor="@color/contact_catalog" />

        <ImageView
            android:id="@+id/iv_year"
            android:layout_width="@dimen/btn_height"
            android:layout_height="@dimen/btn_height"
            android:gravity="center"
            android:src="@drawable/ic_date" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_height"
            android:gravity="center"
            android:text="月份:" />

        <EditText
            android:id="@+id/et_month"
            android:layout_width="@dimen/sp_width"
            android:layout_height="@dimen/btn_height"
            android:enabled="false"
            android:gravity="center"
            android:textColor="@color/contact_catalog" />

        <ImageView
            android:id="@+id/iv_month"
            android:layout_width="@dimen/btn_height"
            android:layout_height="@dimen/btn_height"
            android:gravity="center"
            android:src="@drawable/ic_date" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_height"
            android:gravity="center"
            android:text="日期:" />

        <EditText
            android:id="@+id/et_day"
            android:layout_width="@dimen/sp_width"
            android:layout_height="@dimen/btn_height"
            android:enabled="false"
            android:gravity="center"
            android:textColor="@color/contact_catalog" />

        <ImageView
            android:id="@+id/iv_day"
            android:layout_width="@dimen/btn_height"
            android:layout_height="@dimen/btn_height"
            android:gravity="center"
            android:src="@drawable/ic_date" />
    </LinearLayout>

</LinearLayout>

最后关键代码来了,分别写年、年月、年月日对应DatePickerDialog实现,代码如下:

public class OnPickYearClickListener implements OnClickListener {
    private Context context;
    private EditText editText;

    public OnPickYearClickListener(Context context, EditText editText) {
        this.context = context;
        this.editText = editText;
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);

        setTextYear(year);
    }

    @Override
    public void onClick(View v) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int monthOfYear = c.get(Calendar.MONTH);
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, DatePickerDialog.THEME_HOLO_LIGHT, mDateSetListener, year, monthOfYear, dayOfMonth);
        DatePicker datePicker = datePickerDialog.getDatePicker();
        //隐藏月份
        ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
        //隐藏日
        ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
        datePickerDialog.show();
    }

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

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            setTextYear(year);
        }
    };

    private void setTextYear(int year) {
        editText.setText(new StringBuilder().append(year));
    }

}
public class OnPickMonthClickListener implements OnClickListener {
    private Context context;
    private EditText editText;

    public OnPickMonthClickListener(Context context, EditText editText) {
        this.context = context;
        this.editText = editText;
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int monthOfYear = c.get(Calendar.MONTH);

        setTextMonth(year, monthOfYear);
    }

    @Override
    public void onClick(View v) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int monthOfYear = c.get(Calendar.MONTH);
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, DatePickerDialog.THEME_HOLO_LIGHT, mDateSetListener, year, monthOfYear, dayOfMonth);
        DatePicker datePicker = datePickerDialog.getDatePicker();
        //隐藏月份
        //((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
        //隐藏日
        ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
        datePickerDialog.show();
    }

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

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            setTextMonth(year, monthOfYear);
        }
    };

    private void setTextMonth(int year, int monthOfYear) {
        editText.setText(new StringBuilder()
                .append(year).append("-")
                .append((monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1)));
    }

}
public class OnPickDateClickListener implements OnClickListener {
    private Context context;
    private EditText editText;

    public OnPickDateClickListener(Context context, EditText editText) {
        this.context = context;
        this.editText = editText;
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int monthOfYear = c.get(Calendar.MONTH);
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);

        setTextDate(year, monthOfYear, dayOfMonth);
    }

    @Override
    public void onClick(View v) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int monthOfYear = c.get(Calendar.MONTH);
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
        String dateStr = editText.getText().toString().trim();
        int monthOfYeartemp = monthOfYear + 1;
        String monthOfYeartempStr = String.valueOf(monthOfYeartemp);
        if (monthOfYeartempStr.length() == 1) {
            monthOfYeartempStr = "0" + monthOfYeartempStr;
        }
        String dayOfMonthtempStr = String.valueOf(dayOfMonth);
        if (dayOfMonthtempStr.length() == 1) {
            dayOfMonthtempStr = "0" + dayOfMonth;
        }
        String dateStrCur = year + "-" + monthOfYeartempStr + "-" + dayOfMonthtempStr;
        if (dateStr.equals(dateStrCur) || null == dateStr) {
            setTextDate(year, monthOfYear, dayOfMonth);
            new DatePickerDialog(context, mDateSetListener, year, monthOfYear, dayOfMonth).show();
        } else {
            int year1 = Integer.parseInt(dateStr.substring(0, 4));
            String monthStr = dateStr.substring(5, 7).toString().trim();
            int monthOfYear1 = -1;
            if (monthStr.startsWith("0")) {
                monthStr = monthStr.substring(1, 2);
                monthOfYear1 = Integer.parseInt(monthStr);
                monthOfYear1 = monthOfYear1 - 1;
            } else {
                monthOfYear1 = Integer.parseInt(monthStr);
                monthOfYear1 = monthOfYear1 - 1;
            }
            String dayStr = dateStr.substring(8, 10);
            int dayOfMonth1 = -1;
            if (dayStr.startsWith("0")) {
                dayStr = dayStr.substring(1, 2);
                dayOfMonth1 = Integer.parseInt(dayStr);
            } else {
                dayOfMonth1 = Integer.parseInt(dayStr);
            }

            new DatePickerDialog(context, mDateSetListener, year1, monthOfYear1, dayOfMonth1).show();
        }
    }

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

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            setTextDate(year, monthOfYear, dayOfMonth);
        }
    };

    private void setTextDate(int year, int monthOfYear, int dayOfMonth) {
        editText.setText(new StringBuilder()
                .append(year).append("-")
                .append((monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1)).append("-")
                .append((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth));
    }

}

OK,到这里就结束了。。。。

public class Android_DateActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ private Button button; private TextView textview; private Dialog mdialog; private Calendar calendar = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); initUi(); button.setOnClickListener(this); } public void initUi() { button = (Button) findViewById(R.id.button); textview = (TextView) findViewById(R.id.textview); } @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(0);// 日期弹出框 int SDKVersion = Android_DateActivity.this.getSDKVersionNumber();// 获取系统版本 System.out.println("SDKVersion = " + SDKVersion); DatePicker dp = findDatePicker((ViewGroup) mdialog.getWindow() .getDecorView());// 设置弹出年月日 if (dp != null) { if (SDKVersion 14) { ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)) .getChildAt(1).setVisibility(View.GONE); } } } @Override protected Dialog onCreateDialog(int id) { // 对应上面的showDialog(0);//日期弹出框 mdialog = null; switch (id) { case 0: calendar = Calendar.getInstance(); mdialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { textview.setText(year + "-" + (monthOfYear + 1)); } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); break; } return mdialog; } /** * 从当前Dialog中查找DatePicker子控件 * * @param group * @return */ private DatePicker findDatePicker(ViewGroup group) { if (group != null) { for (int i = 0, j = group.getChildCount()
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值