我有一个转换货币的应用程序,当输入一个值为美元金额时它会起作用但是如果用户没有在textview中输入任何值然后按下它会崩溃的按钮 我该怎么做呢1.它没有崩溃 2.它通过Toast消息告诉用户没有输入任何值
这是我的代码
package com.example.user.currencyconverter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void convert(View view) {
EditText dollarfield = (EditText) findViewById(R.id.dollarfield);
Double dollarAmount = Double.parseDouble(dollarfield.getText().toString());
Double poundAmount = dollarAmount * 0.7;
Toast.makeText(getApplicationContext(), poundAmount.toString() + "Pounds", Toast.LENGTH_LONG).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
答案 0 :(得分:0)
if (dollarfield.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(), "no value has been entered", Toast.LENGTH_LONG).show();
}
else
{
Double dollarAmount = Double.parseDouble(dollarfield.getText().toString());
Double poundAmount = dollarAmount * 0.7;
Toast.makeText(getApplicationContext(), poundAmount.toString() + "Pounds", Toast.LENGTH_LONG).show();
}