1. 程式人生 > >android簡單 實現 日曆,生日 原生簡單輕鬆搞定

android簡單 實現 日曆,生日 原生簡單輕鬆搞定

先看下佈局    對應的一個輸入框點選觸發

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"

        android:gravity="center_vertical"
        android:paddingLeft="8dp"
        android:paddingRight="8dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_weight="3"
            android:text="出生年月"

            android:textSize="16sp" />

        <EditText
            android:id="@+id/personal_info_et_birthday"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="8"



            android:gravity="right|center_vertical"

            android:textSize="16sp" />
    </LinearLayout>


</LinearLayout>

下面主頁面,開始擼程式碼

 

public class PersonalActivity extends AppCompatActivity {
EditText personal_info_et_birthday;
    private int mYear, mMonth, mDay;
    private  String mBirthDay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal);

personal_info_et_birthday = findViewById(R.id.personal_info_et_birthday);
personal_info_et_birthday.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ShowBirthDialog();

    }
});

    }

    private void ShowBirthDialog() {
        Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            new DatePickerDialog(PersonalActivity.this, R.style.DatePickThemeDialog, mDateSetListener, mYear, mMonth, mDay)
                    .show();
        }else new DatePickerDialog(PersonalActivity.this,mDateSetListener, mYear, mMonth, mDay)
                .show();


    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            if (view.isShown()) {
                mYear = year;
                String mm;
                String dd;
                if (monthOfYear < 9) {
                    mMonth = monthOfYear + 1;
                    mm = "0" + mMonth;
                } else {
                    mMonth = monthOfYear + 1;
                    mm = String.valueOf(mMonth);
                }
                if (dayOfMonth < 10) {
                    mDay = dayOfMonth;
                    dd = "0" + mDay;
                } else {
                    mDay = dayOfMonth;
                    dd = String.valueOf(mDay);
                }

                mMonth = monthOfYear;

                mBirthDay = mYear + "-" + mm + "-" + dd;
                Log.e("birthday",mBirthDay.toString());
                personal_info_et_birthday.setText(mBirthDay);
              /*  submitAccountInfo();*/
            }
        }

    };

}

下面就是對應rec檔案下面建立一個values-v21資料夾在裡面建立一個style 這個裡面就是顏色主題 如圖:

style裡面的程式碼

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="DatePickThemeDialog" parent="android:Theme.Material.Light.Dialog">
        <item name="android:colorAccent">@color/theme_color_day</item>
        <item name="android:colorPrimary">@color/theme_color_day</item>
        <item name="android:colorPrimaryDark">@color/theme_color_day</item>

    </style>
</resources>