- 所谓的简单模拟银行转账,就是在自己使用一些布局(目前有三个界面,貌似不堪,用者请见谅,本次只是实现简单模拟的银行转账业务),这模拟有三个页面,分别是:转账前双方账号相关信息输入,确认相关信息页面以及提示是否转账成功(需判断余额)。
- 个人初次看了相关的五大布局以及四大组件(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;
@Override
protected 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() {
@Override
public 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;
@Override
protected 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(){
@Override
public 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() {
@Override
public 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;
@Override
protected 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
时间: 2024-11-01 12:39:18