如何使用 2+ ToggleButtons 获取按钮的总值?

How to use 2+ ToggleButtons to Get the Buttons' Total Value?

我正在尝试制作一个应用程序(我的第一个应用程序)以十进制显示方式显示二进制数的值。我决定使用 ToggleButton 和 1 或 0 的图像来显示它是否被按下。我设法 link '1' 到 TextView 这样当按下 ToggleButton 时它会在其下方显示 "Binary equals: 1" 并且同样显示 0.

我认为添加第二个按钮会很容易,但经过 5(10?)小时的谷歌搜索和试验后,我不知道该怎么做。

到目前为止,我已经尝试了 case breaks but as I have only been programming for two weeks (udacity Android Development for Beginners)我不知道下一步该去哪里,或者我是否在正确的轨道上——例如,ToggleButtons 是个坏主意吗?

我想我需要做的是:

1) 如果选择toggle1,则valueOfOnes = 1,否则valueOfOnes = 0

2) 如果选择 toggle2(最终选择 4、8、16...),则使 valueOfTwos = 2 else = 0

3) 创建一个方法,将 valueOfOnes 添加到 valueOfTwos etc

4) 在 Textview

中显示该值

希望下面的代码看起来不会太乱。它比我之前的大脑更整洁...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.example.android.binary02.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="3">

        <ToggleButton
            android:id="@+id/toggle2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/check"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textOff=""
            android:textOn="" />

        <ToggleButton
            android:id="@+id/toggle1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/check"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textOff=""
            android:textOn="" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Binary equals: "
            android:textSize="50sp" />

        <TextView
            android:id="@+id/decimal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="50sp" />
    </LinearLayout>
</LinearLayout>

CHECK XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, show one -->
<item android:drawable="@drawable/one"
    android:state_checked="true" />
<!-- When not selected, show two-->
<item android:drawable="@drawable/zero"
    android:state_checked="false"/>

</selector>

MainActivity.java:

package com.example.android.binary02;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity
                          implements CompoundButton.OnCheckedChangeListener {
    ToggleButton toggle1;
    ToggleButton toggle2;
    TextView decimalAnswer;
    int valueOfOnes = 1;
    int valueOfTwos = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggle1 = (ToggleButton) findViewById(R.id.toggle1);
        toggle1.setOnCheckedChangeListener(this);

        toggle2 = (ToggleButton) findViewById(R.id.toggle2);
        toggle2.setOnCheckedChangeListener(this);
        decimalAnswer = (TextView) findViewById(R.id.decimal);
    }

    @Override
    public void onCheckedChanged (CompoundButton compoundButton, boolean oneSelected) {
        if(oneSelected) {
            int valueOfOnes = 1;
            addValues(valueOfOnes);
        }
        else
        {
            int valueOfOnes = 0;
            addValues(valueOfOnes);

        }
    }

    /** If this method is commented out and relevant changes made to addValues then one button works fine
     *
     * @param compoundButton
     * @param oneSelected
     */
    @Override
    public void onCheckedChangedTwo (CompoundButton compoundButton, boolean twoSelected) {
        if(twoSelected) {
            int valueOfTwos = 2;
            addValues(valueOfTwos);
        }
        else
        {
            int valueOfTwos = 0;
            addValues(valueOfTwos);
        }
    }


    /**
     * Adds values together.
     *
     * If I delete the int valueOfTwos here and replace with +1 or +0 then the app works (but only for one button it looks like both buttons perform the same action)
     *
     */
    public void addValues(int valueOfOnes, int valueOfTwos) {
        int totalValues;
        totalValues = valueOfOnes + valueOfTwos;
        displayDecimalAnswer(totalValues);
    }

    /**
     * Displays decimal answer.
     */
    public void displayDecimalAnswer(int answer) {
        TextView decimalView = (TextView) findViewById(R.id.decimal);
        decimalView.setText(String.valueOf(answer));
    }
}

已编辑:除了下面 Saran 的回答之外,尝试合并此answer 是否是一个好的路径?

理想情况下,您必须 setOnCheckedChangeListener 切换到两个开关,然后实施一个 onCheckedChanged 方法。然后你必须使用 compoundButton 来检查切换了哪个视图。

你的代码应该是这样的。

@Override
onCreate(){
    toggle1 = (ToggleButton) findViewById(R.id.toggle1);
    toggle2 = (ToggleButton) findViewById(R.id.toggle2);
    toggle1.setOnCheckedChangeListener(this);
    toggle2.setOnCheckedChangeListener(this);
}

@Override
onCheckedChanged (CompoundButton compoundButton, boolean isChecked){
    if(compoundButton == toggle1){
        if(isChecked){
            //Your code if toggle 1 is checked
        } else {
            //Your code if toggle 1 is not checked
        }
    } else {
        if(compoundButton == toggle2){
            if(isChecked){
                //Your code if toggle 2 is checked
            } else {
                //Your code if toggle 2 is not checked
            }
       }
    }
}

而且在您的代码中,addValue 方法的声明采用两个变量。即在 public void addValues(int valueOfOnes, int valueOfTwos) 行中,它采用两个变量,即 valueOfOnesvalueOfTwos.

但是当您调用该方法时,您只传递了一个变量 addValues(valueOfTwos),这就是您收到错误的原因。

如果在声明中声明该方法将接受两个 int 变量,那么在调用该方法时也必须传递两个 int 变量。

请更正,它将解决您的问题。

这就是您保存切换值的方式。

boolean toggle1Status;
boolean toggle2Status;

@Override
onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
    if (compoundButton == toggle1) {

        if (isChecked) {
            //Toggle one was turned on.
            toggle1Status = true;

            if (toggle2Status) {
                //Your code if both the toggles are on
            } else {
                //Your code if toggle 1 is on and 2 is off
            }

        } else {
            //Toggle one was turned off.
            toggle1Status = false;

            if (toggle2Status) {
                //Your code if toggle 1 is off and toggle 2 is on.
            } else {
                //Your code if both the toggles are off.
            }

        }
    } else if (compoundButton == toggle2) {

        if (isChecked) {
            //Toggle two was turned on.
            toggle2Status = true;

            if (toggle1Status) {
                //Your code if both the toggles are on
            } else {
                //Your code if toggle 1 is off and 2 is on
            }

        } else {
            //Toggle two was turned off.
            toggle2Status = false;

            if (toggle1Status) {
                //Your code if toggle 1 is on and toggle 2 is off.
            } else {
                //Your code if both the toggles are off.
            }

        }
    }
}

基本上我们在这里所做的是首先检查已更改的切换状态,然后检查另一个切换状态。除此之外,我们还将当前切换状态的值保存在布尔变量中以供将来使用。

最后结合了下面 Saran 的回答、MRX 的 和我的一些懒惰的固执让它起作用了。

@Override 
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
    if (compoundButton == toggle1) {
        if (isChecked) {
            toggle1Status = true;
        } else { 
            toggle1Status = false;
        } 
    } else if (compoundButton == toggle2) {
        if (isChecked) {
            toggle2Status = true;
        } else { 
            toggle2Status = false;
        } 
    } 
 displayDecimalAnswer();
} 

    /** 
     * Displays decimal answer. 
     */ 
    public void displayDecimalAnswer() {
       int valueOfOnes = (toggle1Status) ? 1 : 0;//Since toggle1Status is class level variable it will be accessibible
       int valueOfTwos = (toggle2Status) ? 2 : 0;
       int answer = valueOfOnes + valueOfTwos;

        TextView decimalView = (TextView) findViewById(R.id.decimal);
        decimalView.setText(""+answer);
    }