Java tutorial
package com.paic.zhifu.wallet.activity.modules.creditpayment; import java.util.ArrayList; import org.json.JSONException; import org.json.JSONObject; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.support.v4.content.LocalBroadcastManager; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import com.paic.zhifu.wallet.activity.R; import com.paic.zhifu.wallet.activity.bean.BankCardInfo; import com.paic.zhifu.wallet.activity.bean.User; import com.paic.zhifu.wallet.activity.cache.DataCache; import com.paic.zhifu.wallet.activity.control.GeneralStructuralActivity; import com.paic.zhifu.wallet.activity.control.widget.InterceptLinearLayout; import com.paic.zhifu.wallet.activity.modules.creditpayment.bean.BankCardInfoPaymentByCredit; import com.paic.zhifu.wallet.activity.modules.creditpayment.net.PaymentByCreditNetOpt; import com.paic.zhifu.wallet.activity.modules.creditpayment.pub.PaymentByCreditIntents; import com.paic.zhifu.wallet.activity.net.HttpConstants; import com.paic.zhifu.wallet.activity.net.inf.ITaskExpandListener; import com.paic.zhifu.wallet.activity.tool.StaticVariableTool; /** * User interface about step one of applying for real name. * * @author Ma Xin * */ public class RealNameSignUpApplyActivity extends GeneralStructuralActivity implements OnClickListener, TextWatcher { // Title bar private TextView titlebarTV; private InterceptLinearLayout titlebarBackLL; private Button signUpApplyBtn; private TextView nameTV; private TextView phoneTV; private TextView certificateTV; private EditText amountET; private TextView amountWarnningTV; private TextView bankcardTV; private ArrayList<BankCardInfo> rechargeBankCards; private ArrayList<BankCardInfoPaymentByCredit> cards = new ArrayList<BankCardInfoPaymentByCredit>(); private String bankcardId = ""; private RelativeLayout moreCardsRL; private AlertDialog bankCardsDialog; private View bankCardsSelectionView; private ListView bankCardsLV; private BankCardSelectionAdapter bankCardsAdapter; private AlertDialog cancelApplyingDialog; private TextView licenseTV; private ProgressDialog progressDialog; private ITaskExpandListener listener = new ITaskExpandListener() { @Override public <T> void onTaskCompleted(int resultCode, T result, int connId) { switch (connId) { case HttpConstants.CONNECTIONID_APPLY_CREDIT_ACCOUNT: if (progressDialog.isShowing()) { progressDialog.dismiss(); } enableApplyBtn(); if (resultCode == 1000) { Intent successStepOne = new Intent( PaymentByCreditIntents.PAYMENTBYCREDIT_STEP_ONE_APPLY_SUCCESSFULLY); LocalBroadcastManager.getInstance(RealNameSignUpApplyActivity.this) .sendBroadcast(successStepOne); Intent gotoStepOneSuccess = new Intent(RealNameSignUpApplyActivity.this, ApplyStepOneSuccessActivity.class); startActivity(gotoStepOneSuccess); finish(); } else { JSONObject object; try { object = new JSONObject(result.toString()); ToastShow(object.optString("resultMsg")); } catch (JSONException e) { e.printStackTrace(); } } break; default: break; } } @Override public void onTaskCanceled() { if (progressDialog.isShowing()) { progressDialog.dismiss(); } enableApplyBtn(); } @Override public void onTaskError(int resultCode, int conId, String msg) { if (progressDialog.isShowing()) { progressDialog.dismiss(); } enableApplyBtn(); } }; @Override public void onClick(View view) { int viewId = view.getId(); switch (viewId) { case R.id.headtitleplus_backParentLayout: showCancelApplyingDialog(); break; case R.id.button_paymentbycredit_register_realapply_agree: String expectedCreditLine = amountET.getText().toString(); if (expectedCreditLine == null || expectedCreditLine.equals("")) { return; } if (bankcardId == null && bankcardId.equals("")) { return; } PaymentByCreditNetOpt.applyPaymentByCreditStepOne(this, bankcardId, Double.valueOf(expectedCreditLine), listener); disableApplyBtn(); break; case R.id.relativelayout_paymentcredit_register_realapply_morecards: if (rechargeBankCards != null && rechargeBankCards.size() > 0) { if (bankCardsDialog == null) { initBankCardsDialog(); } if (!bankCardsDialog.isShowing()) { bankCardsDialog.show(); } } break; case R.id.textview_paymentbycredit_register_apply_license: Intent gotoLicense = new Intent(RealNameSignUpApplyActivity.this, LicenseStepOneActivity.class); startActivity(gotoLicense); } } private void initBankCardsDialog() { for (BankCardInfo bankCardInfo : rechargeBankCards) { BankCardInfoPaymentByCredit card = new BankCardInfoPaymentByCredit(); card.setBankCardId(bankCardInfo.getBankCardId()); card.setBankCardFullName(getBankCardFullName(bankCardInfo)); cards.add(card); } String titleName = getResources().getString(R.string.paymentbycredit_selection_bankcard); bankCardsSelectionView = getLayoutInflater().inflate(R.layout.paymentbycredit_bankcard_selection, null); bankCardsLV = (ListView) bankCardsSelectionView.findViewById(R.id.listview_paymentbycredit_bankcards); bankCardsAdapter = new BankCardSelectionAdapter(this, cards); bankCardsLV.setAdapter(bankCardsAdapter); bankCardsLV.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { bankcardTV.setText(cards.get(position).getBankCardFullName()); bankcardId = cards.get(position).getBankCardId(); bankCardsDialog.dismiss(); } }); bankCardsDialog = new AlertDialog.Builder(this).setTitle(titleName).setView(bankCardsSelectionView) .create(); } private void showCancelApplyingDialog() { if (cancelApplyingDialog == null) { String positiveStr = getResources().getString(R.string.paymentbycredit_applying_cancel); String negativeStr = getResources().getString(R.string.paymentbycredit_applying_continue); String message = getResources().getString(R.string.paymentbycredit_applying_message); cancelApplyingDialog = new AlertDialog.Builder(this).setMessage(message) .setPositiveButton(positiveStr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }).setNegativeButton(negativeStr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create(); cancelApplyingDialog.setCancelable(false); cancelApplyingDialog.setCanceledOnTouchOutside(false); } if (!cancelApplyingDialog.isShowing()) { cancelApplyingDialog.show(); } } @Override public void initIntent() { } @Override public void initControl() { setContentView(R.layout.paymentbycredit_register_realnameapply); titlebarTV = (TextView) findViewById(R.id.headtitleplus_titleText); titlebarTV.setText(getResources().getString(R.string.paymentbycredit_realsignupapply)); titlebarBackLL = (InterceptLinearLayout) findViewById(R.id.headtitleplus_backParentLayout); titlebarBackLL.setOnClickListener(this); signUpApplyBtn = (Button) findViewById(R.id.button_paymentbycredit_register_realapply_agree); signUpApplyBtn.setOnClickListener(this); disableApplyBtn(); amountET = (EditText) findViewById(R.id.edittext_paymentcredit_register_realapply_money); amountWarnningTV = (TextView) findViewById(R.id.textview_paymentcredit_register_realapply_moneywarnning); amountET.addTextChangedListener(this); bankcardTV = (TextView) findViewById(R.id.textview_paymentcredit_register_realapply_bankcard); rechargeBankCards = DataCache.getDataCache().getUser().getBankCardInfos(); moreCardsRL = (RelativeLayout) findViewById(R.id.relativelayout_paymentcredit_register_realapply_morecards); moreCardsRL.setOnClickListener(this); licenseTV = (TextView) findViewById(R.id.textview_paymentbycredit_register_apply_license); licenseTV.setOnClickListener(this); progressDialog = new ProgressDialog(this); progressDialog.setMessage(getString(R.string.paymentbycredit_on_loading)); if (rechargeBankCards != null && rechargeBankCards != null && rechargeBankCards.size() > 0) { bankcardTV.setText(getBankCardFullName(rechargeBankCards.get(0))); bankcardId = rechargeBankCards.get(0).getBankCardId(); } initInfo(); } private String getBankCardFullName(BankCardInfo bankCardInfo) { String bankName = StaticVariableTool.getBankCardNameByTargetName(bankCardInfo.getBankMark()); String bankCardNumber = bankCardInfo.getBankCardNum(); String subA = getResources().getString(R.string.paymentbycredit_selection_bankcardtype_a); String subB = getResources().getString(R.string.paymentbycredit_selection_bankcardtype_b); String fullName = bankName + subA + bankCardNumber.substring(bankCardNumber.length() - 4) + subB; return fullName; } private void initInfo() { nameTV = (TextView) findViewById(R.id.textview_paymentcredit_register_realapply_name); phoneTV = (TextView) findViewById(R.id.textview_paymentcredit_register_realapply_phone); certificateTV = (TextView) findViewById(R.id.textview_paymentcredit_register_realapply_id); User user = DataCache.getDataCache().getUser(); nameTV.setText(user.getRealName()); phoneTV.setText(user.getPhoneNum()); certificateTV.setText(user.getIdCardNo()); } @Override public void initVariable() { } @Override public void initObserver() { } @Override public void jump(int to) { } @Override public void backBtnFunction() { } @Override public void onBackPressed() { showCancelApplyingDialog(); } @Override public void afterTextChanged(Editable editor) { } @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 (!isAvailableAmount(s)) { if (amountWarnningTV.getVisibility() == View.GONE) { amountWarnningTV.setVisibility(View.VISIBLE); } disableApplyBtn(); } else { if (amountWarnningTV.getVisibility() == View.VISIBLE) { amountWarnningTV.setVisibility(View.GONE); } enableApplyBtn(); } } private boolean isAvailableAmount(CharSequence s) { if (s == null || s.toString().equals("")) { return false; } String inputStr = s.toString(); int expectedCreditLine = Integer.valueOf(inputStr); if (!inputStr.equals(String.valueOf(expectedCreditLine))) { amountET.setText(String.valueOf(expectedCreditLine)); } if (expectedCreditLine > PaymentByCreditIntents.PAYMENTBYCREDIT_APPLY_LIMIT_AMOUNT || expectedCreditLine < PaymentByCreditIntents.PAYMENTBYCREDIT_APPLY_LIMIT_AMOUNT_S) { return false; } return true; } private void disableApplyBtn() { signUpApplyBtn.setEnabled(false); signUpApplyBtn.setBackgroundResource(R.drawable.normal_btn2); signUpApplyBtn.setTextColor(getResources().getColor(R.color.btn_word_disable)); } private void enableApplyBtn() { signUpApplyBtn.setEnabled(true); signUpApplyBtn.setBackgroundResource(R.drawable.normal_btn); signUpApplyBtn.setTextColor(Color.WHITE); } }