1. 程式人生 > >Android——簡單模擬銀行轉賬

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;

    @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