我想写一段代码,把卢布转换成美元和欧元。看起来一切都写得很正常,但是转换结果没有输出。在我看来,整个问题都来自于我不知怎么写错了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,但是也没有用
2条答案
按热度按时间jutyujz01#
首先,我不理解assignId的用途,它从来没有被使用过。其次,我认为这样做会更容易:
}
j91ykkif2#
使用这个xml代码和我的代码,这将工作: