在android studio中不输出结果

bzzcjhmw  于 2023-02-20  发布在  Android
关注(0)|答案(2)|浏览(134)

我想写一段代码,把卢布转换成美元和欧元。看起来一切都写得很正常,但是转换结果没有输出。在我看来,整个问题都来自于我不知怎么写错了button Text,或者是一个不正确的条件检查。下面是代码:可扩展标记语言:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">


    <EditText
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:inputType="number"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.275"
        android:id="@+id/solution"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnEur"
        android:layout_width="66dp"
        android:layout_height="72dp"
        android:gravity="center"
        android:text="EUR"
        app:cornerRadius="32dp"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.895"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.525">

    </com.google.android.material.button.MaterialButton>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnUsd"
        android:layout_width="66dp"
        android:layout_height="72dp"
        android:gravity="center"
        android:text="USD"
        android:textSize="15sp"
        app:cornerRadius="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.139"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.525">

    </com.google.android.material.button.MaterialButton>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:gravity="center"
        android:textSize="55sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.996"
        android:id="@+id/result"/>

</androidx.constraintlayout.widget.ConstraintLayout>

哇!

package com.example.currencyconverter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.material.button.MaterialButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView result;
    EditText solution;
    MaterialButton usd, eur;

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

        result = findViewById(R.id.result);
        solution = findViewById(R.id.solution);
        usd = findViewById(R.id.btnUsd);
        eur = findViewById(R.id.btnEur);
    }

    void assignId(MaterialButton btn, int id) {
        btn = findViewById(id);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        MaterialButton button = (MaterialButton) view;
        String buttonTxt = button.toString();

        if (buttonTxt.equals("USD")) {
            String usd = solution.getText().toString();
            double usd1 = Double.parseDouble(usd);
            usd1 = usd1 / 74.76;
            String usd2 = String.valueOf(usd1);
            result.setText(usd2 + " DOLLAR");
        }
        if (buttonTxt.equals("EUR")) {
            String eur = solution.getText().toString();
            double eur1 = Double.parseDouble(eur);
            eur1 = eur1 / 79.61;
            String eur2 = String.valueOf(eur1);
            result.setText(eur2 + " EURO");
        }
    }
}

我试着在每个if后面写return,但是也没有用

jutyujz0

jutyujz01#

首先,我不理解assignId的用途,它从来没有被使用过。其次,我认为这样做会更容易:

TextView result;
EditText solution;
Button usd, eur;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    result = findViewById(R.id.result);
    solution = findViewById(R.id.solution);
    usd = findViewById(R.id.btnUsd);
    eur = findViewById(R.id.btnEur);

    usd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String usd = solution.getText().toString();
            double usd1 = Double.parseDouble(usd);
            usd1 = usd1 / 74.76;
            String usd2 = String.valueOf(usd1);
            result.setText(usd2 + " DOLLAR");
        }
    });

    eur.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String eur = solution.getText().toString();
            double eur1 = Double.parseDouble(eur);
            eur1 = eur1 / 79.61;
            String eur2 = String.valueOf(eur1);
            result.setText(eur2 + " EURO");
        }
    });
}

}

j91ykkif

j91ykkif2#

使用这个xml代码和我的代码,这将工作:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">


    <EditText
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:inputType="number"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.275"
        android:id="@+id/solution"/>

    <Button
        android:id="@+id/btnEur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="72dp"
        android:layout_marginRight="72dp"
        android:layout_marginBottom="112dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/result"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:gravity="center"
        android:textSize="55sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.996"
        android:id="@+id/result"/>

    <Button
        android:id="@+id/btnUsd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="112dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/result"
        app:layout_constraintEnd_toStartOf="@+id/btnEur"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

相关问题