英文:
Display a Toast message to add text instead of the app crashing
问题
以下是您的代码的翻译部分:
public class MainActivity extends AppCompatActivity {
@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 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">
<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>
希望这可以帮助您进行进一步的开发和调试。
英文:
Hey guys I have a question when I add some text in the box it works but when I don't add anything inside, the project crashes. Is there a way to be able to add an if statement to the project to ask the user (via Toast) to add something instead of crashing the app? Thanks
this is the code I have so far
@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);
}
}
this is the xml code I have to set up
<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>```
</details>
# 答案1
**得分**: 1
Yes, you can, all you need is validate the user input before converting it into an `Double` number.
然后,您可能希望在代码周围使用 `try` `catch` 块来防止输入字符串不是数字时出错。
尝试这样做:
```java
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;
}
英文:
Yes, you can, all you need is validate the user input before converting it into an Double
number.
Then, you may want to surround your code with try
catch
block to prevent when the input string is not a number.
Try this:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论