Android implementation puts the year and month selector in AlertDialog

 In the previous article, we learned that Android uses NumberPicker to implement the year and month scrolling selector, connect:

Android uses NumberPicker to implement the year and month scrolling selector - Programmer Sought

In this article, we will talk about how to integrate this year and month scrolling selector into AlertDialog.

The final effect is as follows:

 

 Implementation:

You can set a custom layout in AlertDialog, place the second-level linkage year and month selector in it, and set the click event of the cancel and confirm buttons to obtain the selected year and month data. The modified code is as follows:

MainActivity

package com.example.mouthpickeralertdialog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

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

        // 加载自定义布局
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_date_picker, null);
        final NumberPicker yearPicker = view.findViewById(R.id.yearPicker);
        final NumberPicker monthPicker = view.findViewById(R.id.monthPicker);

       // 设置年份范围
        final 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);
            }
        });

        // 构建AlertDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请选择年月");
        builder.setView(view);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int year = yearPicker.getValue();
                int month = monthPicker.getValue() + 1; // 加1是因为数组下标从0开始
                Log.d("TAG", "Selected year: " + year + ", month: " + month);
                Toast.makeText(MainActivity.this, "您选择的是:" + year + "年" + month + "月", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }
}

Among them, dialog_date_picker is a custom layout file that contains two NumberPickers. You can adjust it according to actual needs. Attach the code of the custom layout file dialog_date_picker.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="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp">

    <NumberPicker
        android:id="@+id/yearPicker"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <NumberPicker
        android:id="@+id/monthPicker"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

In the AlertDialog above, can the cancel and confirm functions be implemented in a custom view (dialog_date_picker), and then hide the confirm and cancel text buttons at the bottom?

The answer is yes, and it is very simple. You can add two more Buttons to the custom layout file above, and hide the OK and Cancel buttons in the AlertDialog. This can be expanded by yourself.

Guess you like

Origin blog.csdn.net/wh445306/article/details/130340720