如何从EditText字段获取值并将其存储在双变量中?
问题描述:
这里是我的代码第一:如何从EditText字段获取值并将其存储在双变量中?
public class MainActivity extends AppCompatActivity
{
Spinner dropdown;
EditText e1; //user enters the bill amount before tip here
TextView tipped; //total tipped amount
TextView Bill; //the total amount of the bill including tax
String sdropdown;
double originalBill = 0; //amount that the user enters converted to a double
double result = 0; //result of multiplying bill and tax
String test1; //editText field has to be converted to a string value first
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); //currency format object
//Create array of items
String tip [] = {"10%", "15%", "20%", "25%", "30%"};
//Create ArrayAdaptor
ArrayAdapter<String> adaptorTipAmount;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create the element
dropdown = (Spinner) findViewById(R.id.tipAmount);
tipped = (TextView) findViewById(R.id.tipTotal);
Bill = (TextView) findViewById(R.id.billTotal);
e1 = (EditText) findViewById(R.id.billAmount);
tipped.setText(currencyFormat.format(0));
Bill.setText(currencyFormat.format(0));
//initialize and set adaptor
adaptorTipAmount = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, tip);
adaptorTipAmount.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropdown.setAdapter(adaptorTipAmount);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
sdropdown = adapter.getItemAtPosition(position).toString();
//what editText field is initially stored in
test1 = e1.getText().toString();
//test1 string variable gets converted to a double
//originalBill = Double.parseDouble(test1);
//determines the tip amount and does the calculation based on the tip
/* if (sdropdown.equals("10%"))
{
result = originalBill * .10;
tipped.setText(Double.toString(result));
}
else if (sdropdown.equals("15%"))
{
result = originalBill * .15;
tipped.setText(Double.toString(result));
}
else if (sdropdown.equals("20%"))
{
result = originalBill * .20;
tipped.setText(Double.toString(result));
}
else if (sdropdown.equals("25%"))
{
result = originalBill * .25;
tipped.setText(Double.toString(result));
}
else if (sdropdown.equals("30%"))
{
result = originalBill * .30;
tipped.setText(Double.toString(result));
}
*/
tipped.setText("The tipped amount is: " + test1);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
}
我遇到的问题是,每当我试图让从的EditText字段中的值,并将其存储在双应用程序崩溃。我搜索了解决这个问题的方法,并且发现如果你先将字段存储在一个字符串中,然后将其转换为一个可以工作的双精度值,那么它不会。我很确定这是我的错误,虽然我没有想法如何解决。我也不想使用按钮。任何帮助?由于
答
试试这个:
如何在最终用途的toString():
tipped.setText(currencyFormat.format(0)).toString();
希望它帮助。
+0
setText()返回void。你不能在它上面调用toString()。这意味着什么? –
答
首先你需要了解这两种类型的区别。 double
是一种原始类型,而Double
是一个对象。在您的全局变量:Double originalBill;
//what editText field is initially stored in
test1 = String.valueOf(e1.getText().toString()); //clear any chance if no string in editText
//test1 string variable gets converted to a double
if(test1 != null)
originalBill = Double.parseDouble(test1);
答
您可能需要清理您从EditText上得到字符串。使用trim()删除任何空格(如果有的话)。
//what editText field is initially stored in
test1 = e1.getText().toString().trim();
看一看什么值存储在字符串
确保它不是空的,如果是使其“0”
if (test1.isEmpty()) test1 = "0";
//test1 string variable gets converted to a double
originalBill = Double.parseDouble(test1);
你能成为一个更具体一点?在哪里它崩溃,什么是异常(如果有的话)。一般来说,我可以看到你做了很多findViewBy Id而没有验证结果。这是在寻求麻烦。将代码包装到try/catch块中并检查异常也有很大帮助。 –
这是崩溃的权利originalBill = parsedouble .... – Ryan
如果我发表评论,只是尝试输出test1这是一个字符串它会工作,但试图输出originalBill将无法正常工作 – Ryan