我试图让我的转换器应用程序运行但我遇到了麻烦。 该应用程序应该接受用户输入,然后,根据用户选择的单选按钮,然后将使用单选按钮特定计算计算用户输入,然后在用户单击“转换”时显示在TextView中#39 ;按钮。
目前,单选按钮选择了计算,但它没有正确计算,我确定它是一个简单的过程来修复它,但我无法找到它。
这是我的代码:
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.MainActivity">
<ImageView
android:id="@+id/imageViewBack"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:srcCompat="@drawable/neo" />
<EditText
android:id="@+id/editTextUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="Enter Amount"
android:inputType="numberDecimal" />
<RadioGroup
android:id="@+id/radioGroupCurrencies"
android:layout_width="match_parent"
android:layout_height="60sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editTextUser"
android:layout_marginTop="20dp">
<RadioButton
android:id="@+id/radioButtonZar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:onClick="onRadioButtonClicked"
android:text="ZAR" />
<RadioButton
android:id="@+id/radioButtonNeo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/radioGroupCurrencies"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:onClick="onRadioButtonClicked"
android:text="NEO" />
</RadioGroup>
<Button
android:id="@+id/buttonConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:text="Convert" />
<Button
android:id="@+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonConvert"
android:layout_alignBottom="@+id/buttonConvert"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="42dp"
android:layout_marginRight="42dp"
android:text="Clear" />
<TextView
android:id="@+id/textViewOutput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonConvert"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="44dp"
android:textSize="30sp" />
</RelativeLayout>
MainActivity:
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.RadioButton;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
RadioButton zar, neo;
Double zarAmount;
Double neoAmount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(final View view) {
final boolean checked = ((RadioButton) view).isChecked();
EditText editTextUser = findViewById(R.id.editTextUser);
final TextView textViewOuput = findViewById(R.id.textViewOutput);
final Double userInput = Double.parseDouble(editTextUser.getText().toString());
zar = findViewById(R.id.radioButtonZar);
neo = findViewById(R.id.radioButtonNeo);
Button btn = findViewById(R.id.buttonConvert);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NumberFormat nm = NumberFormat.getNumberInstance();
switch(view.getId()) {
case R.id.radioButtonZar:
if(checked)
zarAmount = (userInput * 833.44);
textViewOuput.setText(nm.format(zarAmount));
break;
case R.id.radioButtonNeo:
if(checked)
neoAmount = (userInput / 0.001199);
textViewOuput.setText(nm.format(neoAmount));
break;
}
}
});
}
}
如果我做错了什么,请告诉我。感谢。
答案 0 :(得分:0)
您必须在点击按钮
内更改userInput的恢复 final Double userInput = Double.parseDouble(editTextUser.getText().toString());
有onRadioButtonClicked的代码
public void onRadioButtonClicked(final View view) {
final boolean checked = ((RadioButton) view).isChecked();
final EditText editTextUser = findViewById(R.id.editTextUser);
final TextView textViewOuput = findViewById(R.id.textViewOutput);
zar = findViewById(R.id.radioButtonZar);
neo = findViewById(R.id.radioButtonNeo);
Button btn = findViewById(R.id.buttonConvert);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NumberFormat nm = NumberFormat.getNumberInstance();
final Double userInput = Double.parseDouble(editTextUser.getText().toString());
switch(view.getId()) {
case R.id.radioButtonZar:
if(checked)
zarAmount = (userInput * 833.44);
textViewOuput.setText(nm.format(zarAmount));
break;
case R.id.radioButtonNeo:
if(checked)
neoAmount = (userInput / 0.001199);
textViewOuput.setText(nm.format(neoAmount));
break;
}
}
});
}
我还有另外一个观察,你必须验证editTextUser.getText()。toString()在解析Double以防止崩溃之前是否为null
希望这有用:)