소수점 표시의 이진수 값을 보여주는 앱 (내 첫 번째)을 만들려고합니다. 나는 ToggleButton
을 사용했는지 여부를 보여주는 1 또는 0의 이미지와 함께 사용하기로 결정했습니다. "이진과 같다 : 1"나는 ToggleButton
을 누를 때 표시 할 때 그렇게하는 TextView
까지 '1'링크 관리 아래에 마찬가지로 02+ ToggleButtons를 사용하여 버튼의 총 가치를 얻는 방법?
을 위해 나는 두 번째 버튼은 쉽게하지만 5 후 것입니다 추가 생각했다 (10?) 시간의 인터넷 검색 및 실험 나는 그것을 어떻게하는지 전혀 모른다.
나는 case breaks을 시도했지만 난 단지 이주 (udacity Android Development for Beginners 지금까지) 내가 어디 다음 또는 경우에 난 갈 생각이 없다 프로그래밍 된대로 올바른 트랙했다 ToggleButtons
예를 들어, 나쁜 생각? toggle1
을 선택
1) 경우, 다른 valueOfOnes
= 1, valueOfOnes
= 0
2) 만약 toggle2
(결국 4, 8, 16을 : 내가해야 할 일을 생각
이있다 ...) 다른 valueOfTwos
= 2 = 0
3)에 등 valueOfOnes
valueOfTwos
4) 표시하는 값을 추가하는 방법을 확인하게 선택 in 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