Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android——簡單模擬銀行轉賬

Android——簡單模擬銀行轉賬

編輯:關於Android編程

所謂的簡單模擬銀行轉賬,就是在自己使用一些布局(目前有三個界面,貌似不堪,用者請見諒,本次只是實現簡單模擬的銀行轉賬業務),這模擬有三個頁面,分別是:轉賬前雙方賬號相關信息輸入,確認相關信息頁面以及提示是否轉賬成功(需判斷余額)。

個人初次看了相關的五大布局以及四大組件(Activity)、以及Intent的個人運用、使用Handler進行驗證碼倒計時等,那就邊看代碼邊做一些簡單的解釋。

其中布局文件xml有三個:

activity_1.xml




    
    
        
    
    

        
            
            
        

        
            
            
        

        
            
             />
        
    


    
    
        
    
    
        
            
            
        

        
            
             />
        

        
            
            
        
    

    
        
activity_2.xml



    
        
        
    

    
        
        
    

    
    
    


    
        
        
    

    
        
        
    

    
        
        
    

    
        
        
activity_3.xml



    

以上是相關的布局代碼,可以看出本人的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("余額不足");

    }


    }

以上是個人的一些操作,因時間有限(貌似是三個小時做的,故簡陋),其中在判斷余額的順序(先進行相減後的結果,存入與判斷的順序),也希望能幫到你們。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved