2014-10-14 5 views
0

그래서 기본적으로 이것은 매우 간단합니다. 사용자가 식사/서비스에 대한 정보를 입력 할 수있는 팁 계산기 응용 프로그램을 만들고 계산 버튼 옆의 텍스트 필드에서 결과를 계산해야한다고 가정합니다. 내가 가지고있는 문제는 "팁 선택 :"옵션에서 하나의 라디오 버튼 만 선택하는 것입니다. 내 응용 프로그램에 RadioGroup 위젯을 추가하려했지만 내 라디오 버튼을 따르는 방법을 모르거나 현재 라디오 버튼의 크기와 위치에 영향을주지 않으면 서 응용 프로그램에 라디오 그룹을 추가하는 방법을 알지 못합니다. . 여기에 내 응용 프로그램과 같은 모습의 시각이다 : http://tinypic.com/r/yc46t/8라디오 버튼을 선택하는 데 문제가 있습니다

MyActivity.java

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 

     final EditText enteredFoodAmount = (EditText)findViewById(R.id.foodAmount); 
     final EditText enteredDrinkAmount = (EditText)findViewById(R.id.drinkAmount); 
     final EditText enteredTaxAmount = (EditText)findViewById(R.id.taxAmount); 
     final RadioButton tenSelected = (RadioButton)findViewById(R.id.tenPercent); 
     final RadioButton fifteenSelected = (RadioButton)findViewById(R.id.fifteenPercent); 
     final RadioButton twentySelected = (RadioButton)findViewById(R.id.twentyPercent); 
     final TextView displayedFoodAmount = (TextView)findViewById(R.id.theFoodAmount); 
     final TextView displayedDrinkAmount = (TextView)findViewById(R.id.theDrinkAmount); 
     final TextView displayedTaxAmount = (TextView)findViewById(R.id.theTaxAmount); 
     final TextView displayedTipAmount = (TextView)findViewById(R.id.theTipAmount); 
     final Button calculateButton = (Button)findViewById(R.id.calculate); 
     final TextView displayedCalculation = (TextView)findViewById(R.id.calculatedAmount); 
     final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.myRadioGroup); 

     calculateButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       double food = Double.parseDouble(enteredFoodAmount.getText().toString()); 
       double drink = Double.parseDouble(enteredDrinkAmount.getText().toString()); 
       double tax = Double.parseDouble(enteredTaxAmount.getText().toString())/100.0; 
       double tip; 
       double calculation; 

       if (tenSelected.isChecked()){ 
        twentySelected.setChecked(false); 
        tip = 0.10; 
        displayedTipAmount.setText(Double.toString(tip * 100.0) + "%"); 

        displayedFoodAmount.setText("$" + Double.toString(food)); 
        displayedDrinkAmount.setText("$" + Double.toString(drink)); 
        displayedTaxAmount.setText(Double.toString(tax * 100.0) + "%"); 

        double myTax = (food + drink) * tax; 
        double myTip = (food + drink) * tip; 
        calculation = (food + drink) + myTax + myTip; 
        displayedCalculation.setText("$" + Double.toString(calculation)); 
       } 
       else if (twentySelected.isChecked()){ 
        tenSelected.setChecked(false); 
        tip = 0.20; 
        displayedTipAmount.setText(Double.toString(tip * 100.0) + "%"); 

        displayedFoodAmount.setText("$" + Double.toString(food)); 
        displayedDrinkAmount.setText("$" + Double.toString(drink)); 
        displayedTaxAmount.setText(Double.toString(tax * 100.0) + "%"); 

        double myTax = (food + drink) * tax; 
        double myTip = (food + drink) * tip; 
        calculation = (food + drink) + myTax + myTip; 
        displayedCalculation.setText("$" + Double.toString(calculation)); 
       } 
       else if (fifteenSelected.isChecked()){ 

        tip = 0.15; 
        displayedTipAmount.setText(Double.toString(tip * 100.0) + "%"); 

        displayedFoodAmount.setText("$"+ Double.toString(food)); 
        displayedDrinkAmount.setText("$" + Double.toString(drink)); 
        displayedTaxAmount.setText(Double.toString(tax * 100.0) + "%"); 

        double myTax = (food + drink) * tax; 
        double myTip = (food + drink) * tip; 
        calculation = (food + drink) + myTax + myTip; 
        displayedCalculation.setText("$" + Double.toString(calculation)); 
       } 

activity_my.xml 라디오 버튼에은을 radioGroup의 내부 아이들해야

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MyActivity" 
    android:id="@+id/layoutt"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/food_total" 
     android:id="@+id/foodTotal" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/drink_total" 
     android:id="@+id/drinkTotal" 
     android:layout_below="@+id/foodTotal" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="38dp"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/foodAmount" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/drinkAmount" 
     android:layout_alignBottom="@+id/drinkTotal" 
     android:layout_alignLeft="@+id/foodAmount" 
     android:layout_alignStart="@+id/foodAmount"/> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/ten_percent" 
     android:id="@+id/tenPercent" 
     android:layout_toEndOf="@+id/drinkTotal" 
     android:layout_above="@+id/fifteenPercent" 
     android:layout_toRightOf="@+id/drinkTotal"/> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/fifteen_percent" 
     android:id="@+id/fifteenPercent" 
     android:layout_toEndOf="@+id/drinkTotal" 
     android:layout_centerVertical="true" 
     android:layout_alignLeft="@+id/tenPercent" 
     android:layout_alignStart="@+id/tenPercent" 
     android:layout_toRightOf="@+id/drinkTotal"/> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/twenty_percent" 
     android:id="@+id/twentyPercent" 
     android:layout_toEndOf="@+id/drinkTotal" 
     android:layout_below="@+id/fifteenPercent" 
     android:layout_alignLeft="@+id/fifteenPercent" 
     android:layout_alignStart="@+id/fifteenPercent" 
     android:layout_toRightOf="@+id/drinkTotal"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/calculate" 
     android:id="@+id/calculate" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:id="@+id/calculatedAmount" 
     android:layout_alignBottom="@+id/calculate" 
     android:layout_alignLeft="@+id/drinkAmount" 
     android:layout_alignStart="@+id/drinkAmount" 
     android:layout_alignTop="@+id/calculate" 
     android:textSize="22sp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Total Tax" 
     android:id="@+id/tax" 
     android:layout_marginTop="29dp" 
     android:layout_below="@+id/drinkAmount" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/taxAmount" 
     android:layout_alignBottom="@+id/tax" 
     android:layout_alignLeft="@+id/drinkAmount" 
     android:layout_alignStart="@+id/drinkAmount"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/select_tip" 
     android:id="@+id/selectTip" 
     android:layout_above="@+id/tenPercent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="@string/the_food" 
     android:id="@+id/theFood" 
     android:layout_marginTop="37dp" 
     android:layout_below="@+id/twentyPercent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="@string/the_drink" 
     android:id="@+id/theDrink" 
     android:layout_below="@+id/theFood" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="35dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="@string/the_tax" 
     android:id="@+id/theTax" 
     android:layout_alignTop="@+id/theFood" 
     android:layout_toRightOf="@+id/twentyPercent" 
     android:layout_toEndOf="@+id/twentyPercent" 
     android:layout_marginLeft="44dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="@string/the_tip" 
     android:id="@+id/theTip" 
     android:layout_toEndOf="@+id/twentyPercent" 
     android:layout_alignTop="@+id/theDrink" 
     android:layout_alignLeft="@+id/theTax" 
     android:layout_alignStart="@+id/theTax" 
     android:layout_toRightOf="@+id/twentyPercent"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:id="@+id/theFoodAmount" 
     android:layout_toStartOf="@+id/calculatedAmount" 
     android:layout_toRightOf="@+id/calculatedAmount" 
     android:layout_toLeftOf="@+id/calculatedAmount" 
     android:text="--" 
     android:layout_alignTop="@+id/theFood" 
     android:layout_alignLeft="@+id/theDrinkAmount" 
     android:layout_alignStart="@+id/theDrinkAmount"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="--" 
     android:id="@+id/theDrinkAmount" 
     android:layout_toEndOf="@+id/theDrink" 
     android:layout_alignTop="@+id/theDrink" 
     android:layout_toRightOf="@+id/theDrink"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="--" 
     android:id="@+id/theTaxAmount" 
     android:layout_toEndOf="@+id/theTax" 
     android:layout_alignTop="@+id/theTax" 
     android:layout_toRightOf="@+id/theTax"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="--" 
     android:id="@+id/theTipAmount" 
     android:layout_toEndOf="@+id/theTip" 
     android:layout_toLeftOf="@+id/theTip" 
     android:layout_toStartOf="@+id/theTip" 
     android:layout_toRightOf="@+id/theTip" 
     android:layout_alignTop="@+id/theTip" 
     android:layout_alignRight="@+id/calculatedAmount" 
     android:layout_alignEnd="@+id/calculatedAmount"/> 

    <RadioGroup 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignTop="@+id/tenPercent" 
     android:layout_toRightOf="@+id/selectTip" 
     android:layout_toEndOf="@+id/selectTip" 
     android:layout_above="@+id/theFood" 
     android:layout_toLeftOf="@+id/theTip" 
     android:layout_toStartOf="@+id/theTip" 
     android:id="@+id/myRadioGroup" 
     android:baselineAligned="false"> 
    </RadioGroup> 

</RelativeLayout> 

답변

0

.

<RelativeLayout ... > 

    <!-- some views --> 

    <RadioGroup ... > 
     <RadioButton ... /> 

     <RadioButton ... /> 

     <RadioButton ... /> 
    </RadioGroup> 

    <!-- more views --> 

</RelativeLayout> 
+0

내가 자신의 위치에 영향을주지 않고 자녀를 어떻게 키울 수 있는지 알고 있습니까? – Chris

+0

RadioGroup을 이동할 수 있습니다. 내 대답은 시각적 인 것, 계층적인 것만 관련된 것이 아닙니다. – Karakuri

관련 문제