com.app.jdy.ui.CashAdvanceActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.app.jdy.ui.CashAdvanceActivity.java

Source

/**
 * Copyright (c) 2015
 *
 * Licensed under the UCG License, Version 1.0 (the "License");
 */
package com.app.jdy.ui;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.animation.FloatEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.jdy.R;
import com.app.jdy.utils.Constants;
import com.app.jdy.utils.HttpUtils;
import com.app.jdy.utils.StringUtils;
import com.app.jdy.utils.URLs;
import com.app.jdy.widget.WithdrawCashDialog;
import com.umeng.analytics.MobclickAgent;

/**
 * description :
 * 
 * @version 1.0
 * @author liangdong
 * @createtime : 2015-2-3 ?11:14:17
 * 
 *             ?:    --------------- -------------------
 *             ----------------------------------- liangdong 2015-2-3 ?11:14:17
 * 
 */
public class CashAdvanceActivity extends BaseActivity {

    // ????
    String canWithdCash;
    // id
    String memberId;
    // ???
    String money;
    // ??
    String bankCode1;

    Handler handler;
    ImageView backBtn;
    Button button;
    TextView textView;
    TextView textView1;
    TextView textView2;
    TextView textView4;
    TextView textView3;
    TextView textView5;
    EditText editText;
    ArrayList<NameValuePair> params;

    // ???
    WithdrawCashDialog withdrawCashDialog;
    // ??
    BtnConfirmReceiver btnConfirmReceiver;
    // ????
    String withdrawresult;

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

        // ????
        btnConfirmReceiver = new BtnConfirmReceiver();
        IntentFilter intentFilter = new IntentFilter("org.jdy.action.WITHDCASH_BROADCAST");
        CashAdvanceActivity.this.registerReceiver(btnConfirmReceiver, intentFilter);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                // ?????
                case 1:
                    Bundle bundle = msg.getData();

                    canWithdCash = bundle.getString("canWithdCash");
                    String bankName = bundle.getString("bankName");
                    bankCode1 = bundle.getString("bankCode1");
                    String bankCode = bundle.getString("bankCode");

                    if (StringUtils.isNullOrEmpty(bankName)) {
                        bankName = "?";
                    }
                    // ?
                    textView4.setText(bankName);

                    // ?
                    textView3.setText("(" + bankCode + ")");
                    if (StringUtils.isNullOrEmpty(bankCode)) {
                        textView3.setText("");
                    }

                    // ??????
                    textView2.setText(canWithdCash);

                    // ?????
                    editText.setText(canWithdCash);

                    break;

                // ?????
                case 2:
                    if (withdrawCashDialog != null) {
                        withdrawCashDialog.dismiss();
                    }
                    break;

                // ???????
                case 3:
                    Toast.makeText(CashAdvanceActivity.this,
                            "??? " + withdrawresult + " ???", Toast.LENGTH_LONG)
                            .show();

                    editText.setText("");
                    if (withdrawCashDialog != null) {
                        withdrawCashDialog.dismiss();
                    }
                    finish();

                    break;

                // ??????
                case 0x110:
                    Toast.makeText(CashAdvanceActivity.this, "??", Toast.LENGTH_SHORT).show();

                    if (withdrawCashDialog != null) {
                        withdrawCashDialog.dismiss();
                    }
                    break;

                // ????????????
                case 4:
                    Toast.makeText(CashAdvanceActivity.this, Constants.NO_INTENT_TIPS, Toast.LENGTH_LONG).show();
                    if (withdrawCashDialog != null) {
                        withdrawCashDialog.dismiss();
                    }
                    break;
                default:
                    break;
                }

            }
        };
        addEvents();
    }

    Thread withdcashthread = new Thread(new Runnable() {
        @Override
        public void run() {
            params = new ArrayList<NameValuePair>();

            money = editText.getText().toString();

            params.add(new BasicNameValuePair("memId", memberId));
            params.add(new BasicNameValuePair("withdCash", money));
            params.add(new BasicNameValuePair("bankCode", bankCode1));
            withdrawresult = HttpUtils.request(params, URLs.GETCASH_URL);
            if (!withdrawresult.equals("noenough")
                    || !withdrawresult.equals("false") && !withdrawresult.equals("0x110")) {
                Message msg = handler.obtainMessage();
                msg.what = 3;
                handler.sendMessage(msg);
            } else {
                Message msg = handler.obtainMessage();
                msg.what = 0x110;
                handler.sendMessage(msg);
            }

        }
    });
    private ImageView right_img;

    // ????
    private void addEvents() {
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // ????
                if (HttpUtils.isNetworkConnected(CashAdvanceActivity.this)) {

                    if (editText.getText().toString().length() == 0) {
                        Toast.makeText(CashAdvanceActivity.this, "?", Toast.LENGTH_SHORT).show();
                    } else {

                        // ?
                        int count = 0, start = 0;
                        while ((start = editText.getText().toString().indexOf(".", start)) >= 0) {
                            start += ".".length();
                            count++;
                        }
                        if (count > 1 || editText.getText().toString().indexOf(".") == 0) {
                            Toast.makeText(CashAdvanceActivity.this, "", Toast.LENGTH_SHORT)
                                    .show();
                        } else {

                            // ?????
                            BigDecimal judgemoney = null;
                            try {
                                judgemoney = new BigDecimal(editText.getText().toString());
                            } catch (NumberFormatException e) {
                                Toast.makeText(CashAdvanceActivity.this, "",
                                        Toast.LENGTH_SHORT).show();
                                return;
                            }
                            judgemoney = judgemoney.setScale(2, BigDecimal.ROUND_HALF_UP);

                            // ?????
                            String judgecanWithdCash = textView2.getText().toString();

                            if (textView4.getText().toString().equals("?")
                                    || textView4.getText().toString().length() == 0
                                    || textView3.getText().toString().length() == 0) {
                                Toast.makeText(CashAdvanceActivity.this, "?", Toast.LENGTH_SHORT)
                                        .show();
                            } else if (judgemoney.toString() == "0.00") {
                                Toast.makeText(CashAdvanceActivity.this, "?0?",
                                        Toast.LENGTH_SHORT).show();
                            } else if (judgemoney
                                    .compareTo(BigDecimal.valueOf(Double.valueOf(judgecanWithdCash))) == 1) {
                                // BigDecimalcompareTo-1 ? 0
                                // 1
                                Toast.makeText(CashAdvanceActivity.this, "??????",
                                        Toast.LENGTH_LONG).show();
                            } else {
                                editText.setText(judgemoney.toString());

                                withdrawCashDialog = new WithdrawCashDialog(CashAdvanceActivity.this,
                                        R.style.ForwardDialog, judgemoney.toString());
                                withdrawCashDialog.show();
                            }

                        }

                    }
                } else {
                    // ??
                    Toast.makeText(CashAdvanceActivity.this, Constants.NO_INTENT_TIPS, Toast.LENGTH_LONG).show();
                }

            }
        });

        // ??????
        findViewById(R.id.cash_textView5).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CashAdvanceActivity.this, BankCardActivity.class);

                Bundle bundle = new Bundle();
                bundle.putBoolean("isOk", true);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });

    }

    public void initView() {
        backBtn = (ImageView) findViewById(R.id.back_img);
        right_img = (ImageView) findViewById(R.id.right_img);
        backBtn.setVisibility(View.VISIBLE);
        right_img.setVisibility(View.VISIBLE);
        right_img.setBackgroundResource(R.drawable.withdrawcash_history);
        backBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        right_img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getBaseContext(), MyRecordActivity.class));
            }
        });
        textView = (TextView) findViewById(R.id.title_tv);
        textView.setText("??");
        textView1 = (TextView) findViewById(R.id.cash_textView1);
        textView2 = (TextView) findViewById(R.id.cash_textView2);
        textView1.setText("????()");
        textView4 = (TextView) findViewById(R.id.cash_textView4);
        textView5 = (TextView) findViewById(R.id.cash_textView5);
        textView3 = (TextView) findViewById(R.id.cash_textView6);
        editText = (EditText) findViewById(R.id.cash_editext1);

        // ???????
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable edt) {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0)
                    return;
                if (temp.length() - posDot - 1 > 2) {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

        });

        button = (Button) findViewById(R.id.cash_button1);
        // ???
        if (HttpUtils.isNetworkConnected(CashAdvanceActivity.this)) {
            getData();
        } else {
            Toast.makeText(CashAdvanceActivity.this, Constants.NO_INTENT_TIPS, Toast.LENGTH_LONG).show();
        }

    }

    // ?????
    public void getData() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                SharedPreferences userPreferences = getSharedPreferences("umeng_general_config",
                        Context.MODE_PRIVATE);
                memberId = userPreferences.getString("ID", "").trim();

                canWithdCash = HttpUtils.request(null, URLs.CanWithdCash_URL + "/" + memberId);

                String bankCard = HttpUtils.request(null, URLs.BANKCARD_URL + "/" + memberId);

                Message msg = new Message();
                msg.what = 1;
                Bundle bundle = new Bundle();

                try {
                    JSONObject jsonObject = new JSONObject(bankCard);
                    String tmpJson = jsonObject.getString("data");
                    JSONArray jsonArray = new JSONArray(tmpJson);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject tmpObj = (JSONObject) jsonArray.get(i);
                        String bankName = tmpObj.getString("bankName");
                        String bankCode = tmpObj.getString("bankCode");
                        String bankCode1 = tmpObj.getString("bankCode1");

                        bundle.putString("bankName", bankName);
                        bundle.putString("bankCode", bankCode);
                        bundle.putString("bankCode1", bankCode1);
                    }

                } catch (JSONException e) {

                    e.printStackTrace();
                }

                bundle.putString("canWithdCash", canWithdCash);

                msg.setData(bundle);
                handler.sendMessage(msg);

            }
        }).start();
    }

    public class BtnConfirmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getStringExtra("msg").equals("exit")) {
                // ???
                Message msg = handler.obtainMessage();
                msg.what = 2;
                handler.sendMessage(msg);
            } else {
                // ???

                // ?
                if (HttpUtils.isNetworkConnected(CashAdvanceActivity.this)) {
                    // ???
                    withdcashthread.start();

                } else {
                    // ??
                    Message msg = handler.obtainMessage();
                    msg.what = 4;
                    handler.sendMessage(msg);
                }

            }
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}