我在Android中制作了一个简单的小费计算器应用程序,并且我已经完成了所有功能,但我仍然试图修复我发现的错误。目前,您需要在EditText
中输入一个数字,然后从Spinner
中选择服务评级。然后你有2个按钮,一个显示What's my tip?
,另一个显示What's my total with tip?
(每个按钮都是不言自明的)。我发现的错误是,如果您单击EditText
为空的任一按钮,则会导致应用崩溃。我已经尝试了button.setclickable(false/true)
和button.isClickable()
以及button.isEnabled()
,但没有一个有效。也许我确实使用了正确的但没有正确使用它,但我不知道该怎么做。任何帮助将不胜感激。
P.S。抱歉,XML代码没有100%正确格式化,但它全部都在那里
Java代码:
public void calculateTip (View view)
{
String tip = et.getText().toString();
if(tip.isEmpty())
{
btn_tip.setClickable(false);
}
else
{
btn_tip.setClickable(true);
}
double finalTip = Double.parseDouble(tip);
String oneTipFormat = String.format("%.2f", finalTip * 0.10);
String fourTipFormat = String.format("%.2f", finalTip * 0.13);
String sixTipFormat = String.format("%.2f", finalTip * 0.15);
String eightTipFormat = String.format("%.2f", finalTip * 0.20);
String tenTipFormat = String.format("%.2f", finalTip * 0.25);
textView.setVisibility(View.VISIBLE);
String rate = String.valueOf(spin.getSelectedItem());
if(rate.equals("1 star") || rate.equals("2 stars") || rate.equals("3 stars"))
{
String text = "Your rating of: " + rate + " means your tip should be: $" + oneTipFormat;
textView.setText(text);
}
if(rate.equals("4 stars") || rate.equals("5 stars"))
{
String text = "Your rating: " + rate + " means your tip should be: $" + fourTipFormat;
textView.setText(text);
}
if(rate.equals("6 stars") || rate.equals("7 stars"))
{
String text = "Your rating: " + rate + " means your tip should be: $" + sixTipFormat;
textView.setText(text);
}
if(rate.equals("8 stars") || rate.equals("9 stars"))
{
String text = "Your rating: " + rate + " means your tip should be: $" + eightTipFormat;
textView.setText(text);
}
if(rate.equals("10 stars"))
{
String text = "Your rating: " + rate + " means your tip should be: $" + tenTipFormat;
textView.setText(text);
}
}
public void calculateTotal(View v)
{
String value = et.getText().toString();
double finalValue = Double.parseDouble(value);
String onePriceFormat = String.format("%.2f", finalValue + (finalValue * 0.10));
String fourPriceFormat = String.format("%.2f", finalValue + (finalValue * 0.13));
String sixPriceFormat = String.format("%.2f", finalValue + (finalValue * 0.15));
String eightPriceFormat = String.format("%.2f", finalValue + (finalValue * 0.20));
String tenPriceFormat = String.format("%.2f", finalValue + (finalValue * 0.25));
tv.setVisibility(View.VISIBLE);
String rating = String.valueOf(spin.getSelectedItem());
if(rating.equals("1 star") || rating.equals("2 stars") || rating.equals("3 stars"))
{
String text = "Based on your service rating of '" + rating + "', your total with tip should be: $" + onePriceFormat;
tv.setText(text);
}
if(rating.equals("4 stars") || rating.equals("5 stars"))
{
String text = "Based on your service rating of '" + rating + "', your total with tip should be: $" + fourPriceFormat;
tv.setText(text);
}
if(rating.equals("6 stars") || rating.equals("7 stars"))
{
String text = "Based on your service rating of '" + rating + "', your total with tip should be: $" + sixPriceFormat;
tv.setText(text);
}
if(rating.equals("8 stars") || rating.equals("9 stars"))
{
String text = "Based on your service rating of '" + rating + "', your total with tip should be: $" + eightPriceFormat;
tv.setText(text);
}
if(rating.equals("10 stars"))
{
String text = "Based on your service rating of '" + rating + "', your total with tip should be: $" + tenPriceFormat;
tv.setText(text);
}
}
我的XML代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/tv_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/what_is_the_total_price"
android:textSize="24sp"
android:textColor="@android:color/black"/>
<EditText
android:id="@+id/et_bill"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_your_bill_here"
android:gravity="center_horizontal"
android:inputType="numberDecimal"/>
<TextView
android:id="@+id/tv_service"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rate_your_service_using_the_drop_down_menu_below"
android:textSize="24sp"
android:gravity="center"
android:textColor="@android:color/black"/>
<Spinner
android:id="@+id/spinner_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/rating"
android:prompt="@string/service_prompt">
</Spinner>
<Button
android:id="@+id/btn_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/what_s_my_tip"
android:textSize="18sp"
android:layout_margin="25dp"
android:onClick="calculateTip"/>
<TextView
android:id="@+id/tv_tip"
android:layout_marginBottom="20dp"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:visibility="invisible"/>
<Button
android:id="@+id/btn_total"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/what_s_my_total_with_tip"
android:onClick="calculateTotal"/>
<TextView
android:id="@+id/tv_price"
android:layout_margin="10dp"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:visibility="invisible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear"
android:textColor="@android:color/black"
android:textSize="20sp"
android:gravity="center"
android:onClick="clear"/>
</LinearLayout>
答案 0 :(得分:1)
你遇到的问题是你禁用点击处理程序上的按钮点击:)为了禁用按钮,你需要执行逻辑,这样当et
为空时它将失败。 / p>
如果要将et
值与启用/禁用点击按钮相关联,则必须使用TextChangedListener
来检测当前的EditText值。
此外,如果我理解正确,您的按钮应该以禁用开始,因为et
的初始值无效以计算提示。
另一个选项是禁用按钮,而是在if
的开头添加calculateTip
条件。如果et
为空,则显示消息并返回而不执行任何其他代码