java - 如何停用 Android 中的按钮

标签 java android xml button

我正在 Android 中制作一个简单的小费计算器应用程序,并且所有功能都可以正常工作,但我一直在尝试修复我发现的错误。目前发生的情况是,您在 EditText 中输入一个数字,然后从 Spinner 中选择服务评级。然后你有 2 个按钮,一个显示我的小费是多少?,另一个显示我的小费总计是多少?(每个按钮的作用都是不言自明的)。我发现的错误是,如果您在 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 的初始值对于计算小费是无效的。

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

关于java - 如何停用 Android 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166297/

相关文章:

java - 如何从 ListView 中的数字选择器中为不同的项目获取不同的值

java - 无法在 android 中创建 ViewModel 实例

xml - LINQ to XML 至少一个对象必须实现 IComparable

c++ - 用于简单消息的 XML C++ 库

java - Solr DataImportHandler - Oracle 日期 - 夏令时

java - 如何对使用 Scanner 和 InputStream 的方法进行单元测试?

java - Spring Security配置的默认过滤器链是什么?

java - 在Android中分别选取两张图片

javax.crypto.BadPaddingException : pad block corrupted

android - ListView 底部的 TextView - api 7