build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.michaelmuenzer.android:ScrollableNumberPicker:0.2.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } |
drawable/shape_scrollnum
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#cde" /> <corners android:radius="10dp" /> </shape> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<?xml version="1.0" encoding="utf-8"?> <GridLayout 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="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|center_horizontal" android:columnCount="2" android:padding="20dp" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <TextView android:layout_marginRight="10dp" android:text="วันที่" android:textSize="18sp" /> <com.michaelmuenzer.android.scrollablennumberpicker.ScrollableNumberPicker android:id="@+id/number_picker_d" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/shape_scrollnum" android:padding="5dp" app:snp_maxValue="31" app:snp_minValue="1" app:snp_orientation="horizontal" app:snp_value="25" app:snp_value_text_size="18sp" /> <android.support.v4.widget.Space android:layout_width="wrap_content" android:layout_height="30dp" android:layout_columnSpan="2" /> <TextView android:layout_marginRight="10dp" android:text="เดือน" android:textSize="18sp" /> <com.michaelmuenzer.android.scrollablennumberpicker.ScrollableNumberPicker android:id="@+id/number_picker_m" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/shape_scrollnum" android:padding="5dp" app:snp_maxValue="12" app:snp_minValue="1" app:snp_orientation="horizontal" app:snp_value="10" app:snp_value_text_size="18sp" /> <android.support.v4.widget.Space /> <Button android:id="@+id/button" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:text="OK" /> </GridLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.phaisarn.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.Arrays; import java.util.List; import com.michaelmuenzer.android.scrollablennumberpicker.ScrollableNumberPicker; public class MainActivity extends AppCompatActivity { private ScrollableNumberPicker mScrollDate; private ScrollableNumberPicker mScrollMonth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mScrollDate = findViewById(R.id.number_picker_d); mScrollMonth = findViewById(R.id.number_picker_m); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isOK = isOK(); } }); } private boolean isOK() { int d = mScrollDate.getValue(); int m = mScrollMonth.getValue(); List m30 = Arrays.asList(4, 6, 9, 11); //เดือนที่มี 30 วัน if (m == 2 && d > 29) { showToast("เดือนกุมภาพันธ์มีไม่เกิน 29 วัน"); return false; } else if (d == 31 && m30.contains(m)) { //ถ้ากำหนดวันที่เป็น 31 แต่เลือกเดือนที่มี 30 วัน showToast("เดือนที่กำหนดมีไม่เกิน 30 วัน"); return false; } showToast("OK"); return true; } private void showToast(String msg) { Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); } } |