Android使用NumberPicker实现年月滚动选择器

效果截图:

 

在XML布局文件中添加两个NumberPicker

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        android:layout_height="330dp"
        tools:context=".MainActivity" android:background="@android:color/background_light">


    <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center">

        <NumberPicker
                android:id="@+id/yearPicker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        <NumberPicker
                android:id="@+id/monthPicker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
            android:id="@+id/btnGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginBottom="15dp"
            android:layout_gravity="bottom|center">

        <Button
                android:id="@+id/btnCancel"
                android:text="取消"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center" android:layout_marginRight="25dp"/>

        <Button
                android:id="@+id/btnOK"
                android:text="确定"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_gravity="bottom"/>


    </LinearLayout>
    <TextView
            android:id="@+id/textView"
            android:text="请选择账单月份"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</FrameLayout>

在Java代码中获取两个NumberPicker控件,并设置其滚动范围和滚动监听器:

package com.example.mouthpicker;

import android.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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

        final NumberPicker yearPicker = findViewById(R.id.yearPicker);
        final NumberPicker monthPicker = findViewById(R.id.monthPicker);

        // 设置年份范围
        int curYear = Calendar.getInstance().get(Calendar.YEAR);
        yearPicker.setMinValue(curYear - 10);
        yearPicker.setMaxValue(curYear + 10);

        // 设置月份范围
        String[] months = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
        monthPicker.setDisplayedValues(months);
        monthPicker.setMinValue(0);
        monthPicker.setMaxValue(11);

        // 设置滚动监听器
        yearPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int year = picker.getValue();
                int month = monthPicker.getValue() + 1; // 加1是因为数组下标从0开始
                Log.d("TAG", "Selected year: " + year + ", month: " + month);
            }
        });

        monthPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int year = yearPicker.getValue();
                int month = picker.getValue() + 1; // 加1是因为数组下标从0开始
                Log.d("TAG", "Selected year: " + year + ", month: " + month);
            }
        });


        Button button = findViewById(R.id.btnOK);
        //点击事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int year = yearPicker.getValue();
                int month = monthPicker.getValue() + 1; // 加1是因为数组下标从0开始
                Log.d("TAG", "Selected year: " + year + ", month: " + month);
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("提示信息")
                        .setMessage("您选择的是:" + year + "年" + month + "月")
                        .setPositiveButton("确定", null)
                        .show();
            }
        });


    }
}

猜你喜欢

转载自blog.csdn.net/wh445306/article/details/130311738
今日推荐