我正在用android开发一个简单的小费计算器应用程序,并且所有功能都可以正常工作,但是我坚持尝试修复发现的错误。当前发生的情况是,您在EditText中输入数字,然后从Spinner中选择服务等级。然后,您有2个按钮,一个显示What's my tip?,另一个显示What's my total with tip?(每个按钮的作用都是不言自明的)。我发现的错误是,如果在EditText为空的情况下单击任一按钮,则会使应用程序崩溃。我已经尝试过button.setclickable(false/true)button.isClickable()button.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来检测当前的EditText值。

另外,如果我理解正确,您的按钮应该从禁用开始,因为et的初始值对计算笔尖无效。

除了禁用按钮外,另一个选项是在if的最开始处添加calculateTip条件。如果et为空,则显示一条消息并返回而不执行任何其他代码

10-01 05:33
查看更多