Android Studio实现人民币与美元的转换

1.MainActivity

package com.example.flyyu.first2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
     

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

    }

    @Override
    public void onClick(View v) {
     
        EditText editTextren = (EditText)findViewById(R.id.editTextren);
        EditText editTextmei = (EditText)findViewById(R.id.editTextmei);
        EditText editTexthui = (EditText)findViewById(R.id.editTexthui);
        double R1 = 0;
        double M;
        double Huan;
        String a;
        String s;
        String b;
        if (v.getId()==R.id.button){
     
            a=editTextren.getText().toString();
            if("".equals(a)){
     
                R1 = 0;
            }
            else{
     

                R1 =Float.valueOf(a);

            }
            b=editTexthui.getText().toString();
            if("".equals(b)){
     
                Huan = 0;
            }
            else{
     

                Huan =Float.valueOf(b);

            }
            if (Huan==0){
     
                Toast.makeText(MainActivity.this,"输入有误",Toast.LENGTH_SHORT).show();
            }
            else {
     
                M=R1/Huan;
                editTextmei.setText(M+"");
            }

        }
        else{
     
            a=editTextmei.getText().toString();
            if("".equals(a)){
     
                M = 0;
            }
            else{
     

                M =Float.valueOf(a);

            }
            b=editTexthui.getText().toString();
            if("".equals(b)){
     
                Huan = 0;

            }
            else{
     

                Huan =Float.valueOf(b);

            }
            if (Huan==0){
     
                Toast.makeText(MainActivity.this,"输入有误",Toast.LENGTH_SHORT).show();
            }else {
     

                R1=M*Huan;
                editTextren.setText(R1+"");
            }
        }
    }
}

2.activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.flyyu.first2.MainActivity">

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:text="人民币金额:"
                app:layout_constraintBottom_toTopOf="@+id/edittxt_ren"
                app:layout_constraintHorizontal_bias="0.176"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/edittxt_huilv"
                app:layout_constraintVertical_bias="0.812" />

            <EditText
                android:id="@+id/editTextren"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.326"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.181" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="27dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:text="美元兑人民币汇率:"
                app:layout_constraintBottom_toTopOf="@+id/edittxt_huilv"
                app:layout_constraintHorizontal_bias="0.226"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/edittxt_mei"
                app:layout_constraintVertical_bias="1.0" />

            <EditText
                android:id="@+id/editTexthui"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="7dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.326"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.607" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="27dp"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:text="美元金额:"
                app:layout_constraintBottom_toTopOf="@+id/edittxt_mei"
                app:layout_constraintHorizontal_bias="0.182"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.744" />

            <EditText
                android:id="@+id/editTextmei"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.326"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.389" />

        </LinearLayout>

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="39dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="28dp"
            android:onClick="onClick"
            android:text="美元转人民币"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.287"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn_ren"
            app:layout_constraintVertical_bias="0.088" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:onClick="onClick"
            android:text="人民币转美元"
            app:layout_constraintHorizontal_bias="0.29"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/edittxt_ren"
            tools:layout_editor_absoluteY="350dp" />

    </LinearLayout>
</android.support.constraint.ConstraintLayout>

你可能感兴趣的:(android,studio,android)