首先是我的代码:
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字段获取值并将其存储在应用程序崩溃的两倍时。我一直在寻找解决方案,并且发现如果你先将字段存储在一个字符串中,然后将其转换为一个可以工作的双重......那么它没有。我很确定这是我的错误,虽然我不知道如何解决。我也不想使用按钮。有帮助吗?感谢
答案 0 :(得分:0)
试试这个:
如何在结束时使用toString():
<div class="project-slides">
<ul class="slides">
<li class="flex-active-slide"><img src="1"></li>
<li class=""><img src="2"></li>
<li class=""><img src="3"></li>
</ul>
<ul class="flex-direction-nav">
<li class="prev-click">
<a class="flex-prev" href="#">Previous</a>
<div class="prev-thumb same-thumb"></div>
</li>
<li class="next-click">
<a class="flex-next" href="#">Next</a>
<div class="next-thumb same-thumb"></div>
</li>
</ul>
</div>
希望它有所帮助。
答案 1 :(得分:0)
首先,您需要了解这两种类型之间的区别。 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);
答案 2 :(得分:0)
您可能需要清理从EditText获得的String。使用trim()删除任何空格(如果有的话)。
//what editText field is initially stored in
test1 = e1.getText().toString().trim();
查看String中存储的值
Log.v("parseDouble", "test1 = " + test1);
确保它不是空的,如果它是&#34; 0&#34;
if (test1.isEmpty()) test1 = "0";
//test1 string variable gets converted to a double
originalBill = Double.parseDouble(test1);