Android——简单模拟银行转账
所谓的简单模拟银行转账,就是在自己使用一些布局(目前有三个界面,貌似不堪,用者请见谅,本次只是实现简单模拟的银行转账业务),这模拟有三个页面,分别是:转账前双方账号相关信息输入,确认相关信息页面以及提示是否转账成功(需判断余额)。
个人初次看了相关的五大布局以及四大组件(Activity)、以及Intent的个人运用、使用Handler进行验证码倒计时等,那就边看代码边做一些简单的解释。
其中布局文件xml有三个:
activity_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:orientation="vertical"android:background="#F8F8FF"tools:context="com.csii.trans.MainActivity"><!--付款人信息--><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text="付款人信息"android:textColor="#D9D919"android:layout_weight="1"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:background="#F8F8FF"android:layout_weight="4"><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="付款人账号:"android:gravity="left"/><EditText
android:id="@+id/f_et_ac"android:layout_width="match_parent"android:layout_height="wrap_content"android:numeric="signed"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="账 号 余 额:"android:gravity="left"/><TextView
android:id="@+id/f_tv_rest"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#D9D919"android:gravity="center"android:textSize="24dp"android:text="8 000 元"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text="转账金额:"android:gravity="center"/><EditText
android:id="@+id/f_et_trans"android:layout_width="match_parent"android:layout_height="wrap_content"android:numeric="signed"/> /></LinearLayout></LinearLayout><!--收款人信息--><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text="收款人信息"android:gravity="center"android:textColor="#D9D919"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:background="#F8F8FF"android:layout_weight="4"><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="收款方户名:"android:gravity="center"/><EditText
android:id="@+id/f_et_payname"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="收款方账号:"android:gravity="center"/><EditText
android:id="@+id/f_et_payac"android:layout_width="match_parent"android:layout_height="wrap_content"android:numeric="signed"/> /></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text=" 收 款 行 :"android:gravity="center"/><EditText
android:id="@+id/f_et_bank"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"><Button
android:layout_width="match_parent"android:layout_height="wrap_content"android:text="下一步"android:id="@+id/f_btn_next"android:background="#D9D919"android:layout_weight="1"/></LinearLayout></LinearLayout>
- activity_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:orientation="vertical"android:background="#F8F8FF"><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text="付款账号:"/><TextView
android:id="@+id/s_tv_ac"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="left"android:textSize="26dp"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text=" 币 种 :"android:gravity="center"/><TextView
android:id="@+id/s_tv_money"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="26dp"android:text=" 人 民 币 "android:gravity="left"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text="转账金额:"android:gravity="center"/><TextView
android:id="@+id/s_tv_paymon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="26dp"android:gravity="left"/>
</LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text=" 收款户名:"/><TextView
android:id="@+id/s_tv_payname"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="left"android:textSize="24dp"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text=" 收款账户:"android:gravity="center"/><TextView
android:id="@+id/s_tv_payac"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="left"android:textSize="26dp"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="24dp"android:text=" 收 款 行 :"/><TextView
android:id="@+id/s_tv_bank"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="26dp"android:gravity="left"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:gravity="center"android:text="短信验证码:"android:layout_weight="3"/><EditText
android:id="@+id/s_et_mess"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="left"android:textSize="20sp"android:maxLength="10"android:layout_weight="2"android:hint="请输入验证码"/><Button
android:id="@+id/s_btn_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#D9D919"android:textSize="18sp"android:gravity="center_horizontal"android:layout_weight="2"android:text="60s"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><Button
android:id="@+id/s_btn_confirm"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#D9D919"android:onClick="btn_resend"android:text="确认"/></LinearLayout></LinearLayout>
- activity_3.xml
<?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="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.csii.trans.MainActivity"><TextView
android:id="@+id/t_tv_suc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="28dp"android:textColor="#D9D919"/>
</RelativeLayout>
以上是相关的布局代码,可以看出本人的UI不怎么好,有待提高(/汗)。
接下来是涉及到模拟银行转账业务的两个简单的对象类,User(用户)和Pay(转账对象),当然有人说只要一个类就行,因为这模拟的业务太简单了,还没使用到服务器交互数据(这个还在学),个人为了简单点模拟,在页面初步设计时就决定了两个对象的调用的属性不同,都是为了简单模拟下相关的操作。
User.java
package com.csii.trans;import java.io.Serializable;/*** Created by Zeb灬D on 2016/7/16.*/
public class User implements Serializable{private String ac;private int money;public String getAc() {return ac;}public void setAc(String ac) {this.ac = ac;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}
}
Pay.java
package com.csii.trans;import java.io.Serializable;/*** Created by Zeb灬D on 2016/7/16.*/
public class Pay implements Serializable {private String name;private String payac;private String address;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPayac() {return payac;}public void setPayac(String payac) {this.payac = payac;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
以上是两个对象,都实现了Serializable 接口,在使用Intent时需要进行序列化。
下面是对应的三个activity相关操作的类,其中Comfirm类使用Handler与Thread实现倒计时。
MainActivity.java
package com.csii.trans;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private Button f_btn_next;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_1);f_btn_next=(Button) findViewById(R.id.f_btn_next);f_btn_next.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {EditText f_et_ac=(EditText) findViewById(R.id.f_et_ac);TextView f_tv_rest=(TextView) findViewById(R.id.f_tv_rest);EditText f_et_trans=(EditText) findViewById(R.id.f_et_trans);EditText f_et_payname=(EditText) findViewById(R.id.f_et_payname);EditText f_et_payac=(EditText) findViewById(R.id.f_et_payac);EditText f_et_bank=(EditText) findViewById(R.id.f_et_bank);String ac=f_et_ac.getText().toString();String trans=f_et_trans.getText().toString();String name=f_et_payname.getText().toString();String payac=f_et_payac.getText().toString();String bank=f_et_bank.getText().toString();User user=new User();Pay pay=new Pay();if(ac.isEmpty()||trans.isEmpty()||name.isEmpty()||payac.isEmpty()||bank.isEmpty()){Toast.makeText(MainActivity.this, "您还有输入的数据", Toast.LENGTH_SHORT).show();}else{user.setMoney(8000);user.setAc(ac);pay.setName(name);pay.setPayac(payac);pay.setAddress(bank);Intent intent = new Intent(MainActivity.this, Comfirm.class);intent.putExtra("trans", trans);intent.putExtra("user", user);intent.putExtra("pay", pay);startActivity(intent);}}});}}
- Comfirm.java
package com.csii.trans;import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class Comfirm extends AppCompatActivity {private TextView s_tv_ac;private TextView s_tv_money;private TextView s_tv_paymon;private TextView s_tv_payname;private TextView s_tv_payac;private TextView s_tv_bank;private EditText s_et_mess;public static int money;private Button s_btn_time;private Button s_btn_confirm;private User user;private Pay pay;private Handler handler;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_2);s_tv_ac = (TextView) findViewById(R.id.s_tv_ac);s_tv_paymon = (TextView) findViewById(R.id.s_tv_paymon);s_tv_payname = (TextView) findViewById(R.id.s_tv_payname);s_tv_payac = (TextView) findViewById(R.id.s_tv_payac);s_tv_bank = (TextView) findViewById(R.id.s_tv_bank);s_btn_time=(Button)findViewById(R.id.s_btn_time);Intent intent = getIntent();User user = (User) intent.getSerializableExtra("user");Pay pay = (Pay) intent.getSerializableExtra("pay");String trans = intent.getStringExtra("trans");user.setMoney(user.getMoney() - Integer.parseInt(trans, 10));money = user.getMoney() - Integer.parseInt(trans, 10);user.setAc(user.getAc());s_tv_ac.setText(user.getAc());s_tv_paymon.setText(trans);s_tv_payname.setText(pay.getName());s_tv_payac.setText(pay.getPayac());s_tv_bank.setText(pay.getAddress());handler = new Handler() {public void handleMessage(Message msg) {s_btn_time.setText((String)msg.obj);}};
// new Th(handler,Comfirm.this).start();new Thread(new Runnable(){@Overridepublic void run() {for (int i=60;i>=0;i--){Message message=new Message();message.obj=new String(i+"秒");handler.sendMessage(message);try{Thread.sleep(1000);} catch (InterruptedException e){e.printStackTrace();}}}}).start();s_btn_confirm = (Button) findViewById(R.id.s_btn_confirm);s_btn_confirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {s_et_mess=(EditText) findViewById(R.id.s_et_mess);String mess=s_et_mess.getText().toString();if (mess.isEmpty()){Toast.makeText(Comfirm.this, "请输入验证码", Toast.LENGTH_SHORT).show();return;}if (money<0){Toast.makeText(Comfirm.this, "您的余额不足", Toast.LENGTH_SHORT).show();}Intent intent = new Intent(Comfirm.this, Success.class);intent.putExtra("money",String.valueOf(money));intent.putExtra("data", "转账成功");startActivity(intent);}});}}
- Success.java
package com.csii.trans;import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class Success extends AppCompatActivity {private TextView t_tv_suc;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_3);Intent intent=getIntent();t_tv_suc=(TextView)findViewById(R.id.t_tv_suc);int money=Integer.parseInt(intent.getStringExtra("money"),10);if (money>=0) t_tv_suc.setText(intent.getStringExtra("data"));else t_tv_suc.setText("余额不足");}}
- 以上是个人的一些操作,因时间有限(貌似是三个小时做的,故简陋),其中在判断余额的顺序(先进行相减后的结果,存入与判断的顺序),也希望能帮到你们。
May-The-Good-Luck-Be-With-You
Android——简单模拟银行转账相关推荐
- python模拟银行存取款_python 简单模拟银行转账功能
看python教程第三节课的作业 首先画出流程图,流程图与现实代码有出入,因为刚开始画流程图的时候,有些东西没考虑进去,后来写着写着就慢慢能想起来并实现了. 另有一点经验推荐给新手朋友,如果说碰到一个 ...
- 简单的Android程序多少钱,Android简单程序利息计算器
我在编程Android的第一课,并为我的第一次尝试,我试图开发一个利息计算器.该应用程序启动,但然后崩溃.代码如下:Android简单程序利息计算器 package com.example.vitor ...
- Android 关于模拟点击和Hook框架的杂谈
1. 背景 就跟我们以前玩页游挂机一样,Android端也有游戏,而且样式繁多,如果重复的操作每次都要自己重复一遍操作,那不得烦死,所以就有了这篇文章,本文仅探讨方案,大家可以尝试.而且现在的模拟器( ...
- Android之——模拟实现检测心率变化的应用实例
Android之--模拟实现检测心率变化的应用实例 当今,市面上有了一些可以通过Android应用来检测病人心率,血压,体温,等等,一系列方便人们日常生活的Android手机应用.那么,这些实用的手机 ...
- android os开机画面,Android简单实现启动画面的方法
本文实例讲述了Android简单实现启动画面的方法.分享给大家供大家参考,具体如下: 核心代码: package com.demo.app; import android.app.Activity; ...
- IoC容器总结与简单模拟
IoC容器总结与简单模拟 当一个组件需要外部资源时,最直接也最明智的方法是执行查找,这种行为称为主动查找.但这种查找存在一个缺点--组件需要知道如何获得资源.那么它的解决方案是什么呢?请看下文. AD ...
- 使用动态代理简单模拟一下spring的事务管理
按照平时写代码的习惯,我们会定义一个service接口 package com.proxy.test; public interface UserService {public void sayHel ...
- 7-18 银行业务队列简单模拟 (25 分)
7-18 银行业务队列简单模拟 (25 分) 设某银行有A.B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 -- 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客.给定到达 ...
- android 代码浏览,Webview实现android简单的浏览器实例代码
WebView是Android中一个非常实用的组件,它和Safai.Chrome一样都是基于Webkit网页渲染引擎,可以通过加载HTML数据的方式便捷地展现软件的界面,下面通过本文给大家介绍Webv ...
- Jmeter简介以及简单模拟性能测试
1.Jemter简介 1.我们为什么使用Jmeter 开源,免费,基于Java编写,可集成到其他系统可拓展各个功能插件 支持接口测试, 压力(负载和压力)测试等多种功能,支持录制回放, 入门简单相较于 ...
最新文章
- LeetCode简单题之“气球” 的最大数量
- 【Android】Java回调原理并结合Android源码进行理解
- 【深度学习】图像去模糊算法代码实践!
- Android 百度地图开发(一)
- null和undefined相等
- 使用shell脚本或命令行添加、删除 crontab 定时任务
- html语言中glyphicon,Bootstrap字体图标无法正常显示的解决方法
- linux 家目录没有了,linux刀片服务器断电重启以后home目录下的用户文件夹丢失了...
- 豆瓣评分9.3的经典Kubernetes图书推出中文版,学习K8S更方便了
- 【实物】端到端自动驾驶搭建教程(一)附完整资料
- linux 反汇编运行时代码段,linux内核学习之一 简单c语言反汇编(示例代码)
- 图解设计模式-Flyweight模式
- 我们建立数据中心,需要考虑哪些问题?
- 混合动力hev汽车控制模型 simulink stateflow搭建 电机 电池 发动机 模型 动力分配 能量控制策略 及 经济性仿真测试。
- 【TensorFlow】【数字货币】【单词记忆】【微信运营】【DIY电脑】 | Chat · 预告
- compiled.php,laravel compiled.php 缓存 命令行
- 【NDN基础】Information-Centric Networking: A Natural Design for Social Network Applications 全文翻译
- Re: 一著名软件公司的java笔试算法题!
- git push和pull时遇到的两个问题及解决办法
- CVPR2021投稿流程