我正在尝试制作一个应用程序(我的第一个应用程序),该应用程序以十进制显示形式显示二进制数的值。我决定使用带有或图像的ToggleButton
来显示是否按下了它。我设法将'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否则= 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的答案之外,尝试将this 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)
行中,它接受两个变量,分别是valueOfOnes
和valueOfTwos
。但是,当您调用该方法时,您仅传递了一个变量
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.
}
}
}
}
基本上,我们在这里要做的是首先检查已更改的切换状态,然后再检查另一个切换的状态。除此之外,我们还将当前切换状态的值保存在布尔变量中,以备将来使用。