显示一个Toast消息来添加文本,而不是应用程序崩溃。

huangapple go评论94阅读模式
英文:

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(&quot;DefaultLocale&quot;)
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(&quot;%.2f&quot;, enteredNumber);
final String japanFormat = String.format(&quot;%.2f&quot;,converted);
Toast.makeText(MainActivity.this,  &quot;$&quot; +usFormat + &quot; USD is = &quot; +
&quot;&#165;&quot; + japanFormat +  &quot; Yen&quot;, Toast.LENGTH_SHORT).show();
Log.i(&quot;Button pressed&quot;, &quot;Pressed&quot;);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

this is the xml code I have to set up

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;68dp&quot;
android:text=&quot;Enter an amount to convert&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;Button
android:id=&quot;@+id/button2&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:onClick=&quot;convert&quot;
android:text=&quot;Button&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.5&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.254&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/imageView&quot;
android:layout_width=&quot;410dp&quot;
android:layout_height=&quot;287dp&quot;
android:scaleType=&quot;centerInside&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.5&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/button2&quot;
app:srcCompat=&quot;@drawable/money&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/enterNumber&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;7dp&quot;
android:ems=&quot;10&quot;
android:inputType=&quot;numberDecimal&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.5&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;```
</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(&quot;&quot;)) {
Toast.makeText(MainActivity.this,  &quot;Field must not empty&quot;, 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(&quot;%.2f&quot;, enteredNumber);
final String japanFormat = String.format(&quot;%.2f&quot;, converted);
Toast.makeText(MainActivity.this, &quot;$&quot; + usFormat + &quot; USD is = &quot; +
&quot;&#165;&quot; + japanFormat + &quot; Yen&quot;, Toast.LENGTH_SHORT).show();
Log.i(&quot;Button pressed&quot;, &quot;Pressed&quot;);
} catch (NumberFormatException ex) {
Toast.makeText(MainActivity.this,  &quot;Value is not a number!&quot;, Toast.LENGTH_SHORT).show();
return;
}

huangapple
  • 本文由 发表于 2020年8月11日 14:17:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63352518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定