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

时间:2016-07-30 20:54:29

标签: java android togglebutton

我试图制作一个应用程序(我的第一个),以小数显示方式显示二进制数的值。我决定使用ToggleButton,其图像为1或0,表示是否按下了它。我设法链接' 1'最多TextView,以便在按下ToggleButton时显示"二进制等于:1"低于它,同样为0。

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

我已经尝试了case breaks,但由于我只编程了两周(udacity Android Development for Beginners到目前为止),我不知道下一步该去哪,或者我是否在正确的轨道上 - 例如,ToggleButtons是个坏主意吗?

我认为我需要做的是:

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

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

3)制作一个方法,将valueOfOnes添加到valueOfTwos

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是一条很好的路径吗?

2 个答案:

答案 0 :(得分:1)

理想情况下,您必须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.
            }

        }
    }
}

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

答案 1 :(得分:0)

最后,Saran的答案,MRX的here和我的一些懒惰的顽固性的组合使它得以发挥作用。

@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);
    }