简体   繁体   English

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

[英]Display a Toast message to add text instead of the app crashing

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?有没有办法能够向项目添加一个if 语句来要求用户(通过 Toast)添加一些东西而不是让应用程序崩溃? 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这是我必须设置的 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>```

Yes, you can, all you need is validate the user input before converting it into an Double number.是的,您可以,您只需在将用户输入转换为Double数字之前验证用户输入。

Then, you may want to surround your code with try catch block to prevent when the input string is not a number.然后,您可能希望用try catch块包围您的代码,以防止输入字符串不是数字。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM