Java 如何在android中停用按钮
我正在android上制作一个简单的提示计算器应用程序,所有的功能都在运行,但我一直在努力修复我发现的一个bug。当前发生的情况是在Java 如何在android中停用按钮,java,android,xml,button,Java,Android,Xml,Button,我正在android上制作一个简单的提示计算器应用程序,所有的功能都在运行,但我一直在努力修复我发现的一个bug。当前发生的情况是在编辑文本中输入一个数字,然后从微调器中选择一个服务等级。然后你有两个按钮,一个是我的小费是多少?,另一个是我的小费总额是多少?(每个按钮的作用都是不言自明的)。我发现的错误是,如果你在EditText为空的情况下单击任意一个按钮,应用程序就会崩溃。我已经尝试了按钮.setclickable(false/true)和按钮.isClickable()和按钮.isEnab
编辑文本中输入一个数字,然后从微调器中选择一个服务等级。然后你有两个按钮,一个是我的小费是多少?
,另一个是我的小费总额是多少?
(每个按钮的作用都是不言自明的)。我发现的错误是,如果你在EditText
为空的情况下单击任意一个按钮,应用程序就会崩溃。我已经尝试了按钮.setclickable(false/true)
和按钮.isClickable()
和按钮.isEnabled()
,但它们都不起作用。也许我用的是正确的,只是用得不对,但我不知道该怎么办。任何帮助都将不胜感激
另外,很抱歉,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>
您遇到的问题是,您正在禁用单击处理程序上的按钮的单击:),为了禁用按钮,您需要执行逻辑,以便在et
为空时失败
如果要使用启用/禁用单击按钮连接et
值,则必须使用TextChangedListener
检测当前编辑文本值
另外,如果我理解正确,您的按钮应该以disabled开头,因为et
的初始值对于计算小费无效
另一个替代禁用按钮的选项是在CalculateIP
的最开始添加if
条件。如果et
为空,则显示一条消息并返回,而不执行任何其他代码问题可能是由于此double finalTip=double.parseDouble(提示)代码>。尝试放置一个if子句,检查tip是否为空。否则,finalTip将获得值0.0