基本信息
源码名称:androidstudio测式-匯率變換
源码大小:5.33M
文件格式:.zip
开发语言:Java
更新时间:2015-05-22
   源码介绍
使用androidstudio写程式的测试

package com.example.hw1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.util.Log;
import android.widget.RadioButton;
import java.text.DecimalFormat;


public class MainActivity extends ActionBarActivity {

    double money = -1;
    int left = -1, right = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText M = (EditText) findViewById(R.id.editText4);
        M.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                EditText string = (EditText) findViewById(R.id.editText4);
                String temp = string.getText().toString();
                if(temp.isEmpty()) {
                    Button btn = (Button)findViewById(R.id.button);
                    btn.setVisibility(View.INVISIBLE);
                }else{
                    Button button = (Button)findViewById(R.id.button);
                    button.setVisibility(View.VISIBLE);
                }
            }
        });

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText result = (EditText) findViewById(R.id.editText5);
                EditText M = (EditText) findViewById(R.id.editText4);
                String temp = M.getText().toString();
                if(temp.equals("."))
                    money = 0.0;
                else
                    money = Double.valueOf(M.getText().toString());
                if (money < 0) {
                    if (money == -1)
                        result.setText("未輸入數字");
                    else
                        result.setText("請輸入正值");
                } else if (left == -1 || right == -1) {
                    result.setText(left == -1 ? "未輸入原始幣別" : "未輸入欲交換幣別");
                } else {
                    DecimalFormat d = new DecimalFormat("###.###");
                    switch (left) {
                        case 0:
                            switch (right) {
                                case 0:
                                    result.setText(money "");
                                    break;
                                case 1:
                                    money = money / 31;
                                    result.setText(d.format(money) "");
                                    break;
                                case 2:
                                    money = money / 5;
                                    result.setText(d.format(money) "");
                                    break;
                            }
                            break;
                        case 1:
                            switch (right) {
                                case 0:
                                    money = money * 31;
                                    result.setText(money "");
                                    break;
                                case 1:
                                    result.setText(money "");
                                    break;
                                case 2:
                                    money = money * 6;
                                    result.setText(money "");
                                    break;
                            }
                            break;
                        case 2:
                            switch (right) {
                                case 0:
                                    money = money * 5;
                                    result.setText(money "");
                                    break;
                                case 1:
                                    money = money / 6;
                                    result.setText(d.format(money) "");
                                    break;
                                case 2:
                                    result.setText(money "");
                                    break;
                            }
                            break;
                    }
                }
            }
        });
    }
    public void LT(View view){
        left = 0;
    }
    public void LC(View view){
        left = 2;
    }
    public void LA(View view){
        left = 1;
    }

    public void RT(View view){
        right = 0;
    }
    public void RC(View view){
        right = 2;
    }
    public void RA(View view){
        right = 1;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}