自定义对话框

creat_new_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/edit_tag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入分类"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit_money"
        android:hint="请输入金额"/>
    <RadioGroup
        android:id="@+id/radioGroup_in_or_out"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioButton_in"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="收入" />

        <RadioButton
            android:id="@+id/radioButton_out"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="支出" />

    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:gravity="center"
            android:id="@+id/text_date"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="6" />

        <Button
            android:id="@+id/date_choose"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="日期" />
        <TextView
            android:gravity="center"
            android:id="@+id/text_time"
            android:layout_width="0dp"
            android:layout_weight="6"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/time_choose"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:text="时间"/>
    </LinearLayout>
</LinearLayout>

效果:

在HomeFragment.java中使用

    private void showDialog() {
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(Objects.requireNonNull(getContext()));
        @SuppressLint("InflateParams") final View dialogView = LayoutInflater.from(getContext())
                .inflate(R.layout.creat_new_dialog,null);
        final RadioButton radioButton_in=dialogView.findViewById(R.id.radioButton_in);
        Button date_choose_btn=dialogView.findViewById(R.id.date_choose);
        Button time_choose_btn=dialogView.findViewById(R.id.time_choose);
        date_choose_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                mYear = year;
                                mMonth = month;
                                mDay = dayOfMonth;
                                final String data =year+"年"+(month+1) + "月" + dayOfMonth + "日";
                                TextView textView_date=dialogView.findViewById(R.id.text_date);
                                textView_date.setText(data);
                            }
                        },
                        mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });
        time_choose_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog(getContext(),
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                mHour=hourOfDay;
                                mMin=minute;
                                final String data=hourOfDay+":"+minute;
                                TextView textView_date=dialogView.findViewById(R.id.text_time);
                                textView_date.setText(data);
                            }
                        }, mHour,mMin,true).show();
            }
        });
        customizeDialog.setTitle("请输入……");
        customizeDialog.setView(dialogView);
        customizeDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (radioButton_in.isChecked()){
                            // 获取EditView中的输入内容
                            EditText edit_tag = (EditText) dialogView.findViewById(R.id.edit_tag);
                            EditText edit_money=dialogView.findViewById(R.id.edit_money);
                            in_tag_list.add(edit_tag.getText().toString());
                            in_money_list.add(edit_money.getText().toString());
                            in_total_list.add(edit_tag.getText().toString()+edit_money.getText().toString());
                            Save_List(in_money_list,"in_money");
                            Save_List(in_tag_list,"in_tag");
                            Save_List(in_total_list,"in_total");
                            recyclerView_in.setAdapter(mMyAdapter_in);
                        }else{
                            EditText edit_tag = (EditText) dialogView.findViewById(R.id.edit_tag);
                            EditText edit_money=dialogView.findViewById(R.id.edit_money);
                            out_tag_list.add(edit_tag.getText().toString());
                            out_money_list.add(edit_money.getText().toString());
                            out_total_list.add(edit_tag.getText().toString()+edit_money.getText().toString());
                            Save_List(out_money_list,"out_money");
                            Save_List(out_tag_list,"out_tag");
                            Save_List(out_total_list,"out_total");
                            recyclerView_out.setAdapter(myAdapter_out);
                        }
                    }
                });
        customizeDialog.show();
    }

日期及时间选择器(对话框形式)

日期选择器

date_choose_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar=Calendar.getInstance();
                DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                final String data =year+"年"+(month+1) + "月" + dayOfMonth + "日";
                                TextView textView_date=dialogView.findViewById(R.id.text_date);
                                textView_date.setText(data);
                            }
                        },
                        calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });

效果:

时间选择器

time_choose_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar=Calendar.getInstance();
                new TimePickerDialog(getContext(),
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                final String data=hourOfDay+":"+minute;
                                TextView textView_date=dialogView.findViewById(R.id.text_time);
                                textView_date.setText(data);
                            }
                        }, calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
            }
        });

效果:

Last modification:July 21st, 2020 at 10:48 pm