显示Toast消息以添加文本,而不是应用程序崩溃



嘿,伙计们,当我在框中添加一些文本时,我有一个问题,它可以工作,但当我不在框内添加任何内容时,项目就会崩溃。有没有办法在项目中添加if语句,要求用户(通过Toast(添加一些内容,而不是崩溃应用程序?感谢

这是我到目前为止的代码


@SuppressLint("DefaultLocale")
public void convert(View view) {
TextView textView = findViewById(R.id.enterNumber);
double enteredNumber = Double.parseDouble(textView.getText().toString());
double converted = (Double.parseDouble(textView.getText().toString()) * 100)/100*106.111;
final String usFormat = String.format("%.2f", enteredNumber);
final String japanFormat = String.format("%.2f",converted);


Toast.makeText(MainActivity.this,  "$" +usFormat + " USD is = " +
"¥" + japanFormat +  " Yen", Toast.LENGTH_SHORT).show();
Log.i("Button pressed", "Pressed");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

这是我必须设置的xml代码

<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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Enter an amount to convert"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="convert"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.254" />
<ImageView
android:id="@+id/imageView"
android:layout_width="410dp"
android:layout_height="287dp"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:srcCompat="@drawable/money" />
<EditText
android:id="@+id/enterNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>```

可以,您只需要在将用户输入转换为Double数字之前验证用户输入即可。

然后,您可能希望用trycatch块包围代码,以防止输入字符串不是数字。

试试这个:

TextView textView = findViewById(R.id.enterNumber);

String value = textView.getText().toString();
if (value.equals("")) {
Toast.makeText(MainActivity.this,  "Field must not empty", Toast.LENGTH_SHORT).show();
return;
}
try {
double enteredNumber = Double.parseDouble(textView.getText().toString());
double converted = (Double.parseDouble(textView.getText().toString()) * 100) / 100 * 106.111;
final String usFormat = String.format("%.2f", enteredNumber);
final String japanFormat = String.format("%.2f", converted);

Toast.makeText(MainActivity.this, "$" + usFormat + " USD is = " +
"¥" + japanFormat + " Yen", Toast.LENGTH_SHORT).show();
Log.i("Button pressed", "Pressed");
} catch (NumberFormatException ex) {
Toast.makeText(MainActivity.this,  "Value is not a number!", Toast.LENGTH_SHORT).show();
return;
}

最新更新