2016-07-30 52 views
0

我正在嘗試製作一個應用程序(我的第一個),它顯示了十進制顯示中的二進制數值。我決定使用ToggleButton,其中1或0的圖像顯示它是否被按下。我設法鏈接'1'到TextView,這樣當按下ToggleButton時,它顯示「Binary equals:1」在下面,同樣爲0.如何使用2+ ToggleButtons獲取按鈕的總值?

我認爲添加第二個按鈕很容易,但是在5 (10?)小時的谷歌搜索和實驗我不知道該怎麼做。

我試圖case breaks但我只編程了兩個星期(udacity Android Development for Beginners到目前爲止)我不知道在哪裏去下一個,或者如果我在正確的軌道是ToggleButtons一個壞主意,例如?

我想我需要做的是這樣的:

1)如果選擇了toggle1,使valueOfOnes = 1,否則valueOfOnes = 0

2)如果toggle2(最終4,8,16 ...)被選擇使得valueOfTwos = 2否則= 0

3)作出,增加了valueOfOnesvalueOfTwos

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

編輯:除了薩蘭的回答下面就試圖將這一answer是一個很好的路徑可循?

+0

最終的答案都可以找到[這裏](http://stackoverflow.com/questions/38905750/converting-togglebutton-boolean-to-integer-values-then-add-together-in-a-textvie ) – mmmartinnn

回答

0

最後,薩蘭的答案在下面,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); 
    } 
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. 
      } 

     } 
    } 
} 

基本上我們在這裏做的是我們首先檢查被切換的切換狀態,然後檢查另一切換的狀態。此外,我們還將當前切換狀態的值保存在布爾變量中以供將來使用。

+0

非常感謝。絕對更接近! 我在onCheckedChanged之前的'public void'中添加了,是嗎? 現在'2'顯示時,我按切換2和1切換1,但我仍然無法讓addValues方法接受兩個整數。 'public void addValues(int valueOfOnes,int valueOfTwos){totalValues; totalValues = valueOfOnes + valueOfTwos; displayDecimalAnswer(totalValues); }' 給出錯誤MainActivity中的addValues(int,int)不能應用於(int) – mmmartinnn

+0

再次感謝。所以現在我在'onCheckedChange'方法的兩個部分都有'addValues(valueOfOnes,valueOfTwos);'。它自己的每個按鈕都可以正常工作,但'Binary equals:0' textview不會將兩個值相加。它似乎只處理最近按下的按鈕,忘記了另一個的價值。 因此按下切換鍵1它顯示01 = 1.然後按下切換鍵2,我得到11 = 2. 再次按下t1,10 = 0出現。 我試着拿出中間的'else',但這並沒有改變它的運行方式。 – mmmartinnn

+0

如果您想記住切換值,則使用兩個變量來保存每個切換的布爾值。即當開關打開時,'booleanToggle1'應該設置爲true,當點擊開關2時,可以使用** booleanToggle1''變量來確定toggle1 **的狀態。當切換1關閉時,不要忘記將'booleanToggle1'的值重新設置爲false。而你可以爲另一個切換做同樣的事情。 –

相關問題