Java 显示Toast消息以添加文本,而不是应用程序崩溃
嘿,伙计们,我有一个问题,当我在框中添加一些文本时,它可以工作,但是当我没有在框中添加任何内容时,项目崩溃了。是否有一种方法可以向项目中添加if语句,要求用户(通过Toast)添加一些内容,而不是使应用程序崩溃?谢谢 这是我目前掌握的代码Java 显示Toast消息以添加文本,而不是应用程序崩溃,java,android,windows,Java,Android,Windows,嘿,伙计们,我有一个问题,当我在框中添加一些文本时,它可以工作,但是当我没有在框中添加任何内容时,项目崩溃了。是否有一种方法可以向项目中添加if语句,要求用户(通过Toast)添加一些内容,而不是使应用程序崩溃?谢谢 这是我目前掌握的代码 @SuppressLint("DefaultLocale") public void convert(View view) { TextView textView = findViewById(R.id.
@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>```
```
是的,您可以,您所需要的只是在将用户输入转换为双精度
数字之前验证用户输入
然后,您可能希望用try
catch
块包围代码,以防止输入字符串不是数字
试试这个:
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;
}
显示错误日志请显示祝酒词而不是解决基本问题是。。。不雅的(诚实地说,无效的)。你为什么不浏览一下你的日志,找到错误信息,然后编辑你的帖子,把它包括进去呢?想象一下,有一天早上你醒来时头上有一个洞。你认为戴帽子是解决这种问题的正确方法吗?@MarsAtomic根据洞在哪里,思考可能会很困难:)你能给我一个你是如何得到这个的重述吗。我花了一个多小时,仍然无法理解不客气,伙计:)这只是一点经验,当我面对与数字值输入字段相关的问题时。你做android开发多久了?实际上大约一年。令人印象深刻的伙计