按钮不能从 onCheckedChanged 调用,直到我单击 Switch 并再次单击它以 return 到默认状态

Button can't be called from onCheckedChanged until I click Switch and click it agan to return to default state

在我的代码中,我有 2 个 EditBox 和一个 Switch。当我单击切换到 true 时,它会设置可见的线性布局,其中包含一个带有图像 linearLayout.setVisibility(View.VISIBLE);

的 EditText

在底部我有一个计算 2 个案例的按钮:

  1. 当 Switch 为 android:checked="false" 时,它只计算前 2 个 EditBox-s 的值
  2. 当 Switch 为 true 并且我在计算中使用第三个 EditBox 和前两个 Editbox-s 的情况

问题: 当我打开 Activity 并在前 2 个 EditBox 中输入值时,Switchs 默认为 false,我点击 Button 进行计算但没有任何反应。

但是,如果我只是将 Switch 的状态更改为 true 并再次更改为 false。然后我点击按钮,它计算没有问题。

它完全忽略我的代码,直到我改变状态。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


        <EditText
            android:maxLines="1"
            android:padding="12dp"
            android:id="@+id/var1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal"
            android:hint="Probing pulse width (s)"/>

    <EditText
        android:maxLines="1"
        android:padding="12dp"
        android:id="@+id/var2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:imeOptions="actionDone"
        android:inputType="numberDecimal"
        android:hint="Pulse Repetition Time (s)"/>


    <Switch
        android:checked="false"
        android:text="Average transmitting power"
        android:paddingLeft="10dp"
        android:id="@+id/switchID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"/>


<LinearLayout
    android:animateLayoutChanges="true"
    android:id="@+id/hidden"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:paddingTop="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/lambda"/>

    <EditText
        android:maxLines="1"
        android:padding="12dp"
        android:id="@+id/var3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:imeOptions="actionDone"
        android:inputType="numberDecimal"
        android:hint="Pulse Repetition Time (s)"/>


</LinearLayout>

<Button
    android:id="@+id/calculate"
    android:clickable="true"
    android:textColor="@android:color/white"
    style="@style/Widget.AppCompat.Button.Colored"
    android:text="Calculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

<LinearLayout
    android:background="@color/colorAccent"
    android:layout_margin="15dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:padding="5dp"
        android:id="@+id/rezultatID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAlignment="center"
        android:textSize="18sp"
        android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>

MainActivity.java

public class DutyCycle extends AppCompatActivity
{

String naziv;
EditText var1, var2, var3;
Button calculate;
TextView result;
Switch simpleSwitch;
double nResult, nVar1, nVar2, nVar3;
LinearLayout linearLayout;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.duty_cycle_layout);

    naziv = getIntent().getStringExtra("formulica");

    simpleSwitch = (Switch) findViewById(R.id.switchID);

    var1 = (EditText) findViewById(R.id.var1);
    var2 = (EditText) findViewById(R.id.var2);
    var3 = (EditText) findViewById(R.id.var3);
    calculate = (Button) findViewById(R.id.calculate);
    result = (TextView) findViewById(R.id.rezultatID);

    linearLayout = (LinearLayout) findViewById(R.id.hidden);
    linearLayout.setVisibility(View.GONE);

    simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(isChecked)
            {

                calculate.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        if(!var1.getText().toString().equals("") & !var2.getText().toString().equals(""))
                        {
                              nVar1 = Double.parseDouble(var1.getText().toString());
                              nVar2 = Double.parseDouble(var2.getText().toString());
                              nVar3 = Double.parseDouble(!var3.getText().toString().equals("") ? var3.getText().toString() : "0");

                            nResult = (nVar1 / nVar2) * nVar3;


                            result.setText(String.valueOf(nResult) + " [W]");
                        }
                        else
                        {
                            Snackbar.make(findViewById(android.R.id.content), "Please fill in all fields", Snackbar.LENGTH_LONG)
                                    .setAction("CLOSE", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {}
                                    })
                                    .setActionTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_blue_light))
                                    .show();
                        }
                    }
                });
            }
            else
            {
                calculate.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        if(!var1.getText().toString().equals("") & !var2.getText().toString().equals(""))
                        {
                              nVar1 = Double.parseDouble(var1.getText().toString());
                              nVar2 = Double.parseDouble(var2.getText().toString());

                            nResult = (nVar1 / nVar2) * 100;
                            result.setText(String.valueOf(nResult) + " [%]");
                        }
                        else
                        {
                            Snackbar.make(findViewById(android.R.id.content), "Please fill in all fields", Snackbar.LENGTH_LONG)
                                    .setAction("CLOSE", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {}
                                    })
                                    .setActionTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_blue_light))
                                    .show();
                        }
                    }
                });
            }
        }
    });
}
}

我尝试在 onCheckedChanged 中添加一些布尔值,所以如果它第一次没有改变状态,我可以复制 calculate.setOnClickListener 的代码,但它不起作用。

感谢 DroidDev 我现在设法解决了这个问题。我在 onCheckedChanged 之外进行了计算,并使用 if(linearLayout.getVisibility() == View.GONE) 来获取何时使用什么公式。

这是现在的工作代码。希望吧

    simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(isChecked)
            {
                linearLayout.setVisibility(View.VISIBLE);
                linearLayout.setAlpha(0.0f);
                linearLayout.animate()
                        .setDuration(500)
                        .alpha(1.0f)
                        .setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                linearLayout.animate().setListener(null);
                            }
                        });
            }
            else
            {
                linearLayout.animate()
                            .setDuration(300)
                            .alpha(0.0f)
                            .setListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    super.onAnimationEnd(animation);
                                    linearLayout.setVisibility(View.GONE);
                                    linearLayout.animate().setListener(null);
                                }
                            });
            }
        }
    });


    calculate.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(linearLayout.getVisibility() == View.GONE) {
                if (!var1.getText().toString().equals("") & !var2.getText().toString().equals("")) {
                    nVar1 = Double.parseDouble(var1.getText().toString());
                    nVar2 = Double.parseDouble(var2.getText().toString());

                    nResult = (nVar1 / nVar2) * 100;
                    result.setText(String.valueOf(nResult) + " [%]");
                } else {
                    Snackbar.make(findViewById(android.R.id.content), "Please fill in all fields", Snackbar.LENGTH_LONG)
                            .setAction("CLOSE", new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View view) {

                                }
                            })
                            .setActionTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_blue_light))
                            .show();
                }
            }else
            {
                if(!var1.getText().toString().equals("") & !var2.getText().toString().equals(""))
                {
                    nVar1 = Double.parseDouble(var1.getText().toString());
                    nVar2 = Double.parseDouble(var2.getText().toString());
                    nVar3 = Double.parseDouble(!var3.getText().toString().equals("") ? var3.getText().toString() : "0");

                    nResult = (nVar1 / nVar2) * nVar3;


                    result.setText(String.valueOf(nResult) + " [W]");
                }
                else
                {
                    Snackbar.make(findViewById(android.R.id.content), "Please fill in all fields", Snackbar.LENGTH_LONG)
                            .setAction("CLOSE", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {

                                }
                            })
                            .setActionTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_blue_light))
                            .show();
                }
            }
        }
    });