Android 理財計算器

Android 理財計算器

本文原創(chuàng)够傍,轉(zhuǎn)載請注明出處屯蹦。歡迎關(guān)注我的 簡書捎谨。

前言:

最近因工作需要,自己擼了一個計算器Dialog,這邊拿出來跟大家分享下,寫的不好,請多指教
PS:知識點來不及寫了,注釋也沒時間加,大家先看著,后續(xù)有時間再補上

先看效果圖:

自己做的第一個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上調(diào)用方式:

    /**
     * 顯示利率計算器
     */
    private void showOperationDialog() {
        CalculatorFinancialDialog dialog = new CalculatorFinancialDialog(getActivity());
        dialog.show();
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市冕杠,隨后出現(xiàn)的幾起案子矛洞,更是在濱河造成了極大的恐慌洼哎,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,294評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件沼本,死亡現(xiàn)場離奇詭異噩峦,居然都是意外死亡,警方通過查閱死者的電腦和手機抽兆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,493評論 3 385
  • 文/潘曉璐 我一進店門识补,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人辫红,你說我怎么就攤上這事李请。” “怎么了厉熟?”我有些...
    開封第一講書人閱讀 157,790評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長较幌。 經(jīng)常有香客問我揍瑟,道長,這世上最難降的妖魔是什么乍炉? 我笑而不...
    開封第一講書人閱讀 56,595評論 1 284
  • 正文 為了忘掉前任绢片,我火速辦了婚禮,結(jié)果婚禮上岛琼,老公的妹妹穿的比我還像新娘底循。我一直安慰自己,他們只是感情好槐瑞,可當(dāng)我...
    茶點故事閱讀 65,718評論 6 386
  • 文/花漫 我一把揭開白布熙涤。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪祠挫。 梳的紋絲不亂的頭發(fā)上那槽,一...
    開封第一講書人閱讀 49,906評論 1 290
  • 那天,我揣著相機與錄音等舔,去河邊找鬼骚灸。 笑死,一個胖子當(dāng)著我的面吹牛慌植,可吹牛的內(nèi)容都是我干的甚牲。 我是一名探鬼主播,決...
    沈念sama閱讀 39,053評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼蝶柿,長吁一口氣:“原來是場噩夢啊……” “哼丈钙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起只锭,我...
    開封第一講書人閱讀 37,797評論 0 268
  • 序言:老撾萬榮一對情侶失蹤著恩,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蜻展,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體喉誊,經(jīng)...
    沈念sama閱讀 44,250評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,570評論 2 327
  • 正文 我和宋清朗相戀三年纵顾,在試婚紗的時候發(fā)現(xiàn)自己被綠了伍茄。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,711評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡施逾,死狀恐怖敷矫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情汉额,我是刑警寧澤曹仗,帶...
    沈念sama閱讀 34,388評論 4 332
  • 正文 年R本政府宣布,位于F島的核電站蠕搜,受9級特大地震影響怎茫,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜妓灌,卻給世界環(huán)境...
    茶點故事閱讀 40,018評論 3 316
  • 文/蒙蒙 一轨蛤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧虫埂,春花似錦祥山、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,796評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽澳窑。三九已至,卻和暖如春岳颇,著一層夾襖步出監(jiān)牢的瞬間照捡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,023評論 1 266
  • 我被黑心中介騙來泰國打工话侧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留栗精,地道東北人。 一個月前我還...
    沈念sama閱讀 46,461評論 2 360
  • 正文 我出身青樓瞻鹏,卻偏偏與公主長得像悲立,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子新博,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,595評論 2 350

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,827評論 25 707
  • //作者:JRZAlan //備注:第一次做簡書,希望能對大家起到幫助薪夕。 這是對一些計算機編程語言的一些英語單詞,...
    JRZAlan閱讀 16,783評論 0 77
  • 目錄 上一章 柯剛時 “老夫認(rèn)為這件事恐怕沒這么簡單!你和你五師姐兩位落單者都在路途中遭遇了不明強者的截殺赫悄,這明顯...
    步毅閱讀 441評論 0 8
  • 初冬過后原献,太陽已經(jīng)沒有那么熱烈了。正午沒事埂淮,坐在教室門口姑隅,短暫的享受這暖暖的陽光帶來的悠然。 喜歡靜靜的享受這日光...
    淡水云煙2017閱讀 300評論 0 0
  • 看完娜塔莉《不安的時候倔撞,坐下來寫》和《寫出我心》讲仰,有很深的感受,好像記不得讀過了什么痪蝇,但文字間的能量已撫慰心靈鄙陡,令...
    谷應(yīng)閱讀 212評論 0 0