Android 理财计算器

Android 理财计算器

本文原创,转载请注明出处。欢迎关注我的 简书

前言:

最近因工作需要,自己撸了一个计算器Dialog,这边拿出来跟大家分享下,写的不好,请多指教
PS:知识点来不及写了,注释也没时间加,大家先看着,后续有时间再补上

先看效果图:

自己做的第一个GIF

Style:

    <style name="MyDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFullscreen">true</item>
    </style>

    <style name="dialogWindowAnim" mce_bogus="1" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/push_left_in</item>
        <item name="android:windowExitAnimation">@anim/push_left_out</item>
    </style>

Keyboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/white"
    android:keyHeight="10%p"
    android:keyWidth="100%p" >

    <Row>
        <Key
            android:codes="55"
            android:keyEdgeFlags="left"
            android:keyLabel="7" />
        <Key
            android:codes="56"
            android:keyLabel="8" />
        <Key
            android:codes="57"
            android:keyLabel="9" />
    </Row>
    <Row>
        <Key
            android:codes="52"
            android:keyEdgeFlags="left"
            android:keyLabel="4" />
        <Key
            android:codes="53"
            android:keyLabel="5" />
        <Key
            android:codes="54"
            android:keyLabel="6" />
    </Row>
    <Row>
        <Key
            android:codes="49"
            android:keyEdgeFlags="left"
            android:keyLabel="1" />
        <Key
            android:codes="50"
            android:keyLabel="2" />
        <Key
            android:codes="51"
            android:keyLabel="3" />
    </Row>
    <Row>
        <Key
            android:codes="46"
            android:keyEdgeFlags="left"
            android:keyLabel="." />
        <Key
            android:codes="48"
            android:keyLabel="0" />
        <Key
            android:codes="-5"
            android:isRepeatable="true"
            android:keyIcon="@drawable/sym_keyboard_delete" />
    </Row>

</Keyboard>

布局文件,这边就只截图显示,反正大家自己看着搭建就是了:

calculator_dialog

CalculatorFinancialDialog.java:

利率计算方程式我就不贴了,毕竟影响不大

/**
 * Created by caihan on 2017/1/5.
 * 理财计算器(新版)
 */
public class CalculatorFinancialDialog extends Dialog
        implements View.OnClickListener, TextWatcher {
    Activity context;
    private Spinner spinner1;
    private ImageView spinner_performclick;
    private Window window = null;
    private SimpleAdapter simpleAdapter;
    private EditText investment_money;
    private EditText annual_rate;
    private EditText investment_mouth;
    private TextView product_expected_income;
    private TextView bank_expected_income;
    private Reimbursement type = Reimbursement.Dengebenxi;
    private String rate;
    private String mouth;
    private CustomKeyboard mCustomKeyboard;

    public CalculatorFinancialDialog(Activity context) {
        super(context, R.style.MyDialog);
        this.context = context;

    }

    public CalculatorFinancialDialog(Activity context, String rate,
                                     String mouth) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.rate = rate;
        this.mouth = mouth;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.calculator_financial_dialog);
        this.setCanceledOnTouchOutside(false);
        mCustomKeyboard = new CustomKeyboard(this, R.id.keyboardview,
                R.xml.hexkbd);
        simpleAdapter = new SimpleAdapter(context, getData(),
                R.layout.items_spinner, new String[]
                {"TypeName"}, new int[]
                {R.id.textview});
        initWindowView();
        initView();
        initListener();
    }

    @SuppressWarnings("deprecation")
    private void initWindowView() {
        window = getWindow();
        window.setWindowAnimations(R.style.dialogWindowAnim);
        window.getDecorView().setPadding(0, 0, 0, 10);
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
    }

    private void initListener() {
        findViewById(R.id.calculator_dialog_false).setOnClickListener(this);
        spinner_performclick.setOnClickListener(this);
        spinner1.setAdapter(simpleAdapter);
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                switch (position) {
                    case 0:
                        type = Reimbursement.Dengebenxi;
                        break;
                    case 1:
                        type = Reimbursement.Daoqihuanbenhuanxi;
                        break;
                    case 2:
                        type = Reimbursement.Anyuefuxi;
                        break;
                    case 3:
                        type = Reimbursement.Anyuefuxidaoqihuanben;
                        break;
                    default:
                        type = Reimbursement.Dengebenxi;
                        break;
                }
                if (isCanPost()) {
                    product_expected_income
                            .setText(intercept(BorrowCalculateManagerImpl.Bill(
                                    DoubleUtils.toDouble(investment_money.getText().toString()),
                                    DoubleUtils.toDouble(investment_mouth.getText().toString()),
                                    DoubleUtils.toDouble(annual_rate.getText().toString()), type)));

                    bank_expected_income
                            .setText(intercept(BorrowCalculateManagerImpl.Bill(
                                    DoubleUtils.toDouble(investment_money
                                            .getText().toString()),
                                    DoubleUtils.toDouble(investment_mouth
                                            .getText().toString()),
                                    3.0, type)));
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        investment_money
                .addTextChangedListener(new MyTextListener("investment_money"));
        annual_rate.addTextChangedListener(new MyTextListener("annual_rate"));
        investment_mouth
                .addTextChangedListener(new MyTextListener("investment_mouth"));

    }

    private void initView() {
        spinner1 = (Spinner) findViewById(R.id.spinnerBase1);
        spinner_performclick = (ImageView) findViewById(R.id.spinner_performclick);
        investment_money = (EditText) findViewById(R.id.investment_money);
        annual_rate = (EditText) findViewById(R.id.annual_rate);
        investment_mouth = (EditText) findViewById(R.id.investment_mouth);
        product_expected_income = (TextView) findViewById(R.id.product_expected_income);
        bank_expected_income = (TextView) findViewById(R.id.bank_expected_income);
        annual_rate.addTextChangedListener(this);
        mCustomKeyboard.registerEditText(investment_money);
        mCustomKeyboard.registerEditText(annual_rate);
        mCustomKeyboard.registerEditText(investment_mouth);
        investment_mouth.setText(mouth);
        annual_rate.setText(rate);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.spinner_performclick:
                spinner1.performClick();
                break;
            case R.id.calculator_dialog_false:
                dismiss();
                break;

            default:
                break;
        }
    }

    public List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("TypeName", "等额本息");
        list.add(map);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("TypeName", "到期还本还息");
        list.add(map2);
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("TypeName", "按月付息");
        list.add(map3);
        return list;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        TextWatcherUtil.getMoneyFormat(s.toString(), annual_rate);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    class MyTextListener implements TextWatcher {
        private String inputEditView = null;

        public MyTextListener(String inputEditView) {
            this.inputEditView = inputEditView;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,int count) {
            if (s.toString().startsWith(".")
                    || s.toString().matches("[0]{1}[0-9]{1}")) {
                if ("investment_money".equals(inputEditView)) {
                    if (s.toString().matches("[0]{1}[0-9]{1}")) {
                        investment_money.setText("0");
                        investment_money.setSelection(1);
                    } else {
                        investment_money.setText("");
                    }
                    return;
                }
                if ("annual_rate".equals(inputEditView)) {
                    if (s.toString().matches("[0]{1}[0-9]{1}")) {
                        investment_money.setSelection(1);
                        annual_rate.setText("0");
                    } else {
                        annual_rate.setText("");
                    }

                    return;
                }
                if ("investment_mouth".equals(inputEditView)) {
                    if (s.toString().matches("[0]{1}[0-9]{1}")) {
                        investment_money.setSelection(1);
                        investment_mouth.setText("0");
                    } else {
                        investment_mouth.setText("");
                    }

                    return;
                }
            } else {
                if (isCanPost()) {
                    product_expected_income
                            .setText(intercept(BorrowCalculateManagerImpl.Bill(
                                    DoubleUtils.toDouble(investment_money
                                            .getText().toString()),
                                    DoubleUtils.toDouble(investment_mouth
                                            .getText().toString()),
                                    DoubleUtils.toDouble(
                                            annual_rate.getText().toString()),
                                    type)));

                    bank_expected_income
                            .setText(intercept(BorrowCalculateManagerImpl.Bill(
                                    DoubleUtils.toDouble(investment_money
                                            .getText().toString()),
                                    DoubleUtils.toDouble(investment_mouth
                                            .getText().toString()),
                                    3.0, type)));
                } else {
                    product_expected_income.setText("0");
                    bank_expected_income.setText("0");
                }

            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }

    }

    private String intercept(String account) {
        String number = StringUtils.getDoubleFormat(account);
        return number + "元";
    }

    public boolean isCanPost() {
        return (!StringUtils.isEmpty2(investment_money.getText().toString())
                && !StringUtils.isEmpty2(investment_mouth.getText().toString())
                && !StringUtils.isEmpty2(annual_rate.getText().toString()));
    }
}

CustomKeyboard.java

/**
 * Created by caihan on 2017/1/5.
 * 自定义键盘
 */
public class CustomKeyboard {

    private KeyboardView mKeyboardView;
    private Dialog mHostActivity;

    private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener() {

        public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
        public final static int CodeCancel = -3; // Keyboard.KEYCODE_CANCEL

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
            if (focusCurrent == null ||
                    (focusCurrent.getClass() != EditText.class && focusCurrent.getClass() != AppCompatEditText.class)) {
                return;
            }
            EditText edittext = (EditText) focusCurrent;
            Editable editable = edittext.getText();
            int start = edittext.getSelectionStart();
            if (primaryCode == CodeCancel) {
                hideCustomKeyboard();
            } else if (primaryCode == CodeDelete) {
                if (editable != null && start > 0)
                    editable.delete(start - 1, start);
            } else {
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }

        @Override
        public void onPress(int arg0) {
        }

        @Override
        public void onRelease(int primaryCode) {
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeRight() {
        }

        @Override
        public void swipeUp() {
        }
    };

    public CustomKeyboard(Dialog calculatorDialog, int viewid, int layoutid) {
        mHostActivity = calculatorDialog;
        mKeyboardView = (KeyboardView) calculatorDialog.findViewById(viewid);
        mKeyboardView.setKeyboard(
                new Keyboard(mHostActivity.getContext(), layoutid));
        mKeyboardView.setPreviewEnabled(false); // 显示白板
        mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
    }

    /**
     * 隐藏.
     */
    public void hideCustomKeyboard() {
        mKeyboardView.setVisibility(View.GONE);
        mKeyboardView.setEnabled(false);
    }

    public void registerEditText(int resid) {
        EditText edittext = (EditText) mHostActivity.findViewById(resid);
        edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
            }
        });
        edittext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        edittext.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                EditText edittext = (EditText) v;
                int inType = edittext.getInputType();
                edittext.setInputType(InputType.TYPE_NULL);
                edittext.onTouchEvent(event);
                edittext.setInputType(inType);
                return true;
            }
        });
        edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }

    public void registerEditText(EditText edittext) {
        edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
            }
        });
        edittext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        edittext.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                EditText edittext = (EditText) v;
                int inType = edittext.getInputType();
                edittext.setInputType(InputType.TYPE_NULL);
                edittext.onTouchEvent(event);
                edittext.setInputType(inType);
                return true;
            }
        });
        edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }

}

Activity上调用方式:

    /**
     * 显示利率计算器
     */
    private void showOperationDialog() {
        CalculatorFinancialDialog dialog = new CalculatorFinancialDialog(getActivity());
        dialog.show();
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,657评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,662评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,143评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,732评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,837评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,036评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,126评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,868评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,315评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,641评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,773评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,859评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,584评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,676评论 2 351

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,856评论 25 707
  • //作者:JRZAlan //备注:第一次做简书,希望能对大家起到帮助。 这是对一些计算机编程语言的一些英语单词,...
    JRZAlan阅读 16,790评论 0 77
  • 目录 上一章 柯刚时 “老夫认为这件事恐怕没这么简单!你和你五师姐两位落单者都在路途中遭遇了不明强者的截杀,这明显...
    步毅阅读 441评论 0 8
  • 初冬过后,太阳已经没有那么热烈了。正午没事,坐在教室门口,短暂的享受这暖暖的阳光带来的悠然。 喜欢静静的享受这日光...
    淡水云烟2017阅读 300评论 0 0
  • 看完娜塔莉《不安的时候,坐下来写》和《写出我心》,有很深的感受,好像记不得读过了什么,但文字间的能量已抚慰心灵,令...
    谷应阅读 212评论 0 0