我正在嘗試製作一個應用程序(我的第一個),它顯示了十進制顯示中的二進制數值。我決定使用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)作出,增加了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));
}
}
編輯:除了薩蘭的回答下面就試圖將這一answer是一個很好的路徑可循?
最終的答案都可以找到[這裏](http://stackoverflow.com/questions/38905750/converting-togglebutton-boolean-to-integer-values-then-add-together-in-a-textvie ) – mmmartinnn