Android_下方弹出菜单的实现

时间:2022-08-04 17:50:29

这一功能要用到动画相关知识

实现点击按钮弹出下方输入框,这里点击可弹出一个输入界面,其中包括一个小型计算器。

点击date可弹出datedialog设置date。

 Android_下方弹出菜单的实现

 

 

 

1、编写弹出框的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="500dp"
   >
    <EditText
        android:id="@ id/tv_tell"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/riple_btn_nocorner_3"
        android:textColor="#212"
        android:gravity="right"
        android:hint="备注"
        android:textColorHint="#B1A1A1"
        android:textSize="30dp"></EditText>
    <TextView
        android:id="@ id/tv_cord"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/tv_tell"
        android:background="@drawable/riple_btn_nocorner_3"
        android:gravity="right"
        android:textColor="#212"
        android:hint="0"
        android:textColorHint="#B1A1A1"
        android:textSize="30dp"></TextView>

    <TextView
        android:id="@ id/tv_calcul"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@ id/tv_cord"
        android:background="@drawable/riple_btn_nocorner_3"
        android:textColor="#212"
        android:gravity="right"
        android:hint="0"
        android:textColorHint="#B1A1A1"
        android:textSize="30dp"></TextView>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.3dp"
        android:background="#212"
        ></View>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tv_calcul"
        android:orientation="vertical"
        android:gravity="center">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"

            ><Button
            android:id="@ id/calcul_btn_ac"
            android:layout_width="140dp"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp"
            android:background="@drawable/riple_btn_nocorner_3"
            android:fontFamily="宋体"
            android:text="AC"
            android:textSize="25dp"
            android:layout_weight="2"
            android:textColor="#169FDF"
            ></Button>

            <Button
                android:id="@ id/calcul_btn_date"
                android:layout_width="210dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:fontFamily="宋体"
                android:text="date"
                android:textAllCaps="false"
                android:layout_weight="3"
                android:textColor="#169FDF"
                android:textSize="25dp"></Button>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:orientation="horizontal"
            >
            <Button
                android:id="@ id/calcul_btn_1"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:fontFamily="宋体"
                android:text="1"
                android:layout_weight="1"
                android:textColor="#169FDF"
                android:textSize="25dp"></Button>
            <Button
                android:id="@ id/calcul_btn_2"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="2"
                android:layout_weight="1"
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_3"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="3"
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:layout_weight="1"
                android:fontFamily="宋体"></Button>

            <Button
                android:id="@ id/calcul_btn_del"
                android:layout_width="140dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="del"
                android:textSize="30dp"
                android:textColor="#169FDF"
                android:layout_weight="2"
                android:textAllCaps="false"
                android:fontFamily="宋体"></Button>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:orientation="horizontal"
            >

            <Button
                android:id="@ id/calcul_btn_5"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="5"
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:layout_weight="1"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_6"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="6"
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:layout_weight="1"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_4"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="4"
                android:textSize="25dp"
                android:layout_weight="1"
                android:textColor="#169FDF"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_add"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text=" "
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:fontFamily="宋体"
                android:layout_weight="1"
                ></Button>
            <Button
                android:id="@ id/calcul_btn_multiply"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="×"
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:fontFamily="宋体"></Button></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:orientation="horizontal"
            >

            <Button
                android:id="@ id/calcul_btn_7"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_below="@id/calcul_btn_4"
                android:layout_marginTop="0dp"
                android:layout_toLeftOf="@id/calcul_btn_5"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="7"
                android:textSize="25dp"
                android:layout_weight="1"
                android:textColor="#169FDF"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_8"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_below="@id/calcul_btn_5"
                android:layout_marginTop="0dp"
                android:layout_toRightOf="@id/calcul_btn_7"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="8"
                android:layout_weight="1"
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_9"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_below="@id/calcul_btn_6"
                android:layout_marginTop="0dp"
                android:layout_toRightOf="@id/calcul_btn_8"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="9"
                android:layout_weight="1"
                android:textColor="#169FDF"
                android:textSize="25dp"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_sub"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="-"
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:layout_weight="1"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_divide"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:layout_weight="1"
                android:text="÷"
                android:textSize="25dp"
                android:textColor="#169FDF"
                android:fontFamily="宋体"></Button>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:orientation="horizontal"
            >
            <Button
                android:id="@ id/calcul_btn_point"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_below="@id/calcul_btn_9"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="."
                android:gravity="center"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:textSize="40dp"
                android:textColor="#169FDF"
                android:fontFamily="宋体"></Button>
            <Button
                android:id="@ id/calcul_btn_0"
                android:layout_width="140dp"
                android:layout_height="match_parent"
                android:layout_below="@id/calcul_btn_8"
                android:layout_marginTop="0dp"
                android:layout_toRightOf="@id/calcul_btn_7"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="0"
                android:textSize="25dp"
                android:fontFamily="宋体"
                android:layout_weight="2"
                android:textColor="#169FDF"
                ></Button>

            <Button
                android:id="@ id/calcul_btn_equal"
                android:layout_width="140dp"
                android:layout_height="match_parent"
                android:layout_below="@id/calcul_btn_divide"
                android:layout_marginTop="0dp"
                android:background="@drawable/riple_btn_nocorner_3"
                android:text="完成"
                android:textSize="20dp"
                android:layout_weight="2"
                android:textColor="#169FDF"
                android:fontFamily="宋体"></Button>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

  

2、编写一个Java类实现其弹出框中的各项功能,囊括一个接口使其在activity中实现

package com.example.didida_corder;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.icu.util.Calendar;
import android.icu.util.ChineseCalendar;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

//import java.util.Calendar;
import java.util.Date;
import java.util.zip.Inflater;

import static java.lang.Math.sqrt;

public class CalculFragment {
    private Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_add,
            btn_del, btn_mul, btn_ac, btn_sub, btn_equal, btn_divide, btn_date,
            btn_point;
    private TextView tv_cord, tv_calcul;
    private double init = 0;//当前输入数据
    private char calcul = ‘#‘;//运算符
    private double nowsum = 0;//当前运算结果
    private int pointnum = 0;//小数点输入次数
    private RelativeLayout root;
    private Context context;
    boolean isaheadchar = false, isnowpoint = false, isaheadequal = false, isaheadsqr = false;

    //isaheadchar: 是否输入过运算符
    //isnowpoint:小数点状态
    //isaheadequal:前一个运算符是否为=
    //isaheadsqr:前一个运算符是否为平方,主要为setText方便而设置
    @RequiresApi(api = Build.VERSION_CODES.N)
    public CalculFragment(RelativeLayout root, Context context) {
        this.root = root;
        this.context = context;
        bindandset(this.root);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public RelativeLayout bindandset(View view) {
        btn_0 = root.findViewById(R.id.calcul_btn_0);
        btn_1 = root.findViewById(R.id.calcul_btn_1);
        btn_2 = root.findViewById(R.id.calcul_btn_2);
        btn_3 = root.findViewById(R.id.calcul_btn_3);
        btn_4 = root.findViewById(R.id.calcul_btn_4);
        btn_5 = root.findViewById(R.id.calcul_btn_5);
        btn_6 = root.findViewById(R.id.calcul_btn_6);
        btn_7 = root.findViewById(R.id.calcul_btn_7);
        btn_8 = root.findViewById(R.id.calcul_btn_8);
        btn_9 = root.findViewById(R.id.calcul_btn_9);
        btn_date = root.findViewById(R.id.calcul_btn_date);
        btn_point = root.findViewById(R.id.calcul_btn_point);
        btn_sub = root.findViewById(R.id.calcul_btn_sub);
        btn_ac = root.findViewById(R.id.calcul_btn_ac);
        btn_add = root.findViewById(R.id.calcul_btn_add);
        btn_equal = root.findViewById(R.id.calcul_btn_equal);
        btn_del = root.findViewById(R.id.calcul_btn_del);
        btn_divide = root.findViewById(R.id.calcul_btn_divide);
        btn_mul = root.findViewById(R.id.calcul_btn_multiply);
        tv_calcul = root.findViewById(R.id.tv_calcul);
        tv_cord = root.findViewById(R.id.tv_cord);
        tv_calcul.setText("");
        tv_cord.setText("");
        btn_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                } else tv_calcul.setText(tv_calcul.getText()   "0");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("1");
                } else tv_calcul.setText(tv_calcul.getText()   "1");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("2");
                } else tv_calcul.setText(tv_calcul.getText()   "2");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("3");
                } else tv_calcul.setText(tv_calcul.getText()   "3");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("4");
                } else tv_calcul.setText(tv_calcul.getText()   "4");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("5");
                } else tv_calcul.setText(tv_calcul.getText()   "5");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("6");
                } else tv_calcul.setText(tv_calcul.getText()   "6");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("7");
                } else tv_calcul.setText(tv_calcul.getText()   "7");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("8");
                } else tv_calcul.setText(tv_calcul.getText()   "8");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal || isaheadchar) {
                    btn_equal.setText("=");
                    tv_calcul.setText("9");
                } else tv_calcul.setText(tv_calcul.getText()   "9");
                init = Double.parseDouble((String) tv_calcul.getText());
                isaheadequal = false;
                isaheadchar = false;
            }
        });
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (calcul) {
                    case ‘ ‘: {
                        nowsum  = init;
                        break;
                    }
                    case ‘-‘: {
                        nowsum -= init;

                        break;
                    }
                    case ‘*‘: {
                        nowsum *= init;

                        break;
                    }
                    case ‘/‘: {
                        if (init != 0) {
                            nowsum /= init;
                        } else
                            break;
                    }
                    default: {
                        nowsum  = init;
                    }
                }
                if (isaheadchar) {
                    if (!isaheadsqr)
                        tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
                    calcul = ‘ ‘;

                }
                if (!isaheadequal) {
                    if (!isaheadchar)
                        tv_cord.setText(tv_cord.getText()   ""   init   " ");
                    else tv_cord.setText(tv_cord.getText()   " ");
                    tv_calcul.setText(""   nowsum);
                } else {
                    tv_cord.setText(""   nowsum   " ");
                    tv_calcul.setText(""   nowsum);
                }
                calcul = ‘ ‘;
                init = 0;
                isaheadequal = false;
                isaheadchar = true;
                isaheadsqr = false;
                isnowpoint = false;
                pointnum = 0;
            }
        });
        btn_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal) {
                    tv_cord.setText("");
                }
                switch (calcul) {
                    case ‘ ‘: {
                        nowsum  = init;

                        break;
                    }
                    case ‘-‘: {
                        nowsum -= init;

                        break;
                    }
                    case ‘*‘: {
                        nowsum *= init;
                        break;
                    }
                    case ‘/‘: {
                        if (init != 0) {
                            nowsum /= init;
                        } else
                            break;
                    }
                    case ‘士‘: {
                        nowsum *= -1;
                        break;
                    }
                    case ‘%‘: {
                        nowsum %= init;
                        break;
                    }
                    default: {
                        nowsum  = init;
                    }
                }
                if (isaheadchar) {
                    if (!isaheadsqr)
                        tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
                    calcul = ‘-‘;
                }
                if (!isaheadequal) {
                    if (!isaheadchar)
                        tv_cord.setText(tv_cord.getText()   ""   init   "-");
                    else tv_cord.setText(tv_cord.getText()   "-");
                    tv_calcul.setText(""   nowsum);
                } else {
                    tv_cord.setText(""   nowsum   "-");
                    tv_calcul.setText(""   nowsum);
                }
                calcul = ‘-‘;
                init = 0;
                isaheadequal = false;
                isaheadchar = true;
                isaheadsqr = false;
                isnowpoint = false;
                pointnum = 0;
            }
        });
        btn_mul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal) {
                    tv_cord.setText("");
                }
                switch (calcul) {
                    case ‘ ‘: {
                        nowsum  = init;
                        break;
                    }
                    case ‘-‘: {
                        nowsum -= init;
                        break;
                    }
                    case ‘*‘: {
                        nowsum *= init;
                        break;
                    }
                    case ‘/‘: {
                        if (init != 0) {
                            nowsum /= init;
                        } else
                            break;
                    }
                    case ‘士‘: {
                        nowsum *= -1;
                        break;
                    }
                    case ‘%‘: {
                        nowsum %= init;
                        break;
                    }
                    default: {
                        nowsum  = init;
                    }
                }
                //if ptr calcul is calcul change it
                if (isaheadchar) {
                    if (!isaheadsqr)
                        tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
                    calcul = ‘*‘;
                }
                if (!isaheadequal) {
                    if (!isaheadchar)
                        tv_cord.setText(tv_cord.getText()   ""   init   "×");
                    else tv_cord.setText(tv_cord.getText()   "×");
                    tv_calcul.setText(""   nowsum);
                } else {
                    tv_cord.setText(""   nowsum   "×");
                    tv_calcul.setText(""   nowsum);
                }
                calcul = ‘*‘;
                init = 0;
                isaheadchar = true;
                isaheadequal = false;
                isaheadsqr = false;
                isnowpoint = false;
                pointnum = 0;
            }
        });
        btn_divide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal) {
                    tv_cord.setText("");
                }
                switch (calcul) {
                    case ‘ ‘: {
                        nowsum  = init;
                        break;
                    }
                    case ‘-‘: {
                        nowsum -= init;
                        break;
                    }
                    case ‘*‘: {
                        nowsum *= init;
                        break;
                    }
                    case ‘/‘: {
                        if (init != 0) {
                            nowsum /= init;
                        } else
                            break;
                    }
                    case ‘士‘: {
                        nowsum *= -1;
                        break;
                    }
                    case ‘%‘: {
                        nowsum %= init;
                        break;
                    }
                    default: {
                        nowsum  = init;
                    }
                }
                if (isaheadchar) {
                    if (!isaheadsqr)
                        tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
                    calcul = ‘/‘;
                }
                if (!isaheadequal) {
                    if (!isaheadchar)
                        tv_cord.setText(tv_cord.getText()   ""   init   "÷");
                    else tv_cord.setText(tv_cord.getText()   "÷");
                    tv_calcul.setText(""   nowsum);
                } else {

                    tv_cord.setText(""   nowsum   "÷");
                    tv_calcul.setText(""   nowsum);
                }
                calcul = ‘/‘;
                init = 0;
                isaheadequal = false;
                isaheadchar = true;
                isaheadsqr = false;
                isnowpoint = false;
                pointnum = 0;
            }
        });
        btn_point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_calcul.setText(tv_calcul.getText()   ".");
                isnowpoint = true;
            }
        });
        btn_equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isaheadequal) {
                    tv_cord.setText("");
                }
                switch (calcul) {
                    case ‘ ‘: {
                        nowsum  = init;
                        break;
                    }
                    case ‘-‘: {
                        nowsum -= init;
                        break;
                    }
                    case ‘*‘: {
                        nowsum *= init;

                        break;
                    }
                    case ‘/‘: {
                        if (init != 0) {
                            nowsum /= init;
                        } else
                            break;
                    }
                    case ‘士‘: {
                        nowsum *= -1;
                        break;
                    }
                    case ‘%‘: {
                        nowsum %= init;
                        break;
                    }
                    default: {
                        nowsum = init;
                    }
                }
                if (!isaheadsqr)
                    tv_cord.setText(tv_cord.getText()   ""   init   "=");
                else tv_cord.setText(tv_cord.getText()   "=");
                tv_calcul.setText(""   nowsum);
                calcul = ‘#‘;
                init = 0;
                btn_equal.setText("完成");
                isaheadequal = true;
                isaheadsqr = false;
                isnowpoint = false;
                pointnum = 0;
            }
        });
        btn_ac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_calcul.setText("");
                tv_cord.setText("");
                calcul = ‘#‘;
                init = 0;
                nowsum = 0;
                isaheadchar = false;
                isnowpoint = false;
                isaheadequal = false;
                isaheadsqr = false;
                pointnum = 0;
            }
        });
        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_calcul.getText().length() != 0)
                    tv_calcul.setText(tv_calcul.getText().subSequence(0, tv_calcul.getText().length() - 1));
            }
        });
        final Calendar calendar = Calendar.getInstance();
        String time = ""   calendar.get(Calendar.YEAR)   "/"   (calendar.get(Calendar.MONTH)   1)   "/"   calendar.get(Calendar.DAY_OF_MONTH);
        btn_date.setText(time);
        btn_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar1=ChineseCalendar.getInstance();
                DatePickerDialog datePickerDialog=new DatePickerDialog(context, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        btn_date.setText("" year "/" month "/" dayOfMonth);
                    }
                }, calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });
        return (RelativeLayout) view;
    }
}

  

3、Activity中实现2中的接口

public void send(int n) {
        Dialog mCameraDialog = new Dialog(this, R.style.BottomDialog);
        RelativeLayout root = (RelativeLayout) LayoutInflater.from(this).inflate(
                R.layout.layout_fragment_calcul, null);
        CalculFragment calculFragment=new CalculFragment(root,MainActivity.this);
        root=calculFragment.bindandset(root);
        mCameraDialog.setContentView(root);
        Window dialogWindow = mCameraDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.DialogAnimation); // 添加动画
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = 0; // 新位置Y坐标
        lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
        root.measure(0, 0);
        lp.height = root.getMeasuredHeight();
//        lp.alpha = 9f; // 透明度
        dialogWindow.setAttributes(lp);
        mCameraDialog.show();
    }

  style

<style name="BottomDialog" parent="@android:style/Theme.Dialog">

        <!-- 背景透明 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <!-- 浮于Activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- Dialog以外的区域模糊效果 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 无标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- Dialog进入及退出动画 -->
        <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
    </style>
    <!-- ActionSheet进出动画 -->
    <style name="DialogAnimation" parent="@android:style/Animation.Dialog">
        <item name="android:windowEnterAnimation">@anim/dialog_in</item>
        <item name="android:windowExitAnimation">@anim/dialog_out</item>
    </style>

  

相关参考:

https://blog.csdn.net/lyp_1020k/article/details/79858982?utm_source=app