0

calcBtnを計算するにはどうすればよいですか?現在、ラジオボタンをクリックするとプログラムが自動的に計算し、リセットボタンは正常に機能しますが、計算ボタンは何もしません。

javaファイル

import java.text.DecimalFormat;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener{

EditText et1;
EditText et2;
EditText et3;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
RadioGroup rg1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et1=(EditText)findViewById(R.id.billText);
    et2=(EditText)findViewById(R.id.tipText);
    et3=(EditText)findViewById(R.id.totalText);
    rb1=(RadioButton)findViewById(R.id.five);
    rb2=(RadioButton)findViewById(R.id.ten);
    rb3=(RadioButton)findViewById(R.id.fifteen);
    rb4=(RadioButton)findViewById(R.id.twenty);
    rg1=(RadioGroup)findViewById(R.id.radioGroup1);
    rg1.setOnCheckedChangeListener(this);

}

public void onCheckedChanged(RadioGroup rg1, int i) {

    // TODO Auto-generated method stub
    if (i==rb1.getId()){
        double Bill = Double.parseDouble(et1.getText().toString());
        double Tip = Bill * .05;
        double Total = Tip + Bill;
        DecimalFormat df = new DecimalFormat("#.00");
        et2.setText(df.format(Tip));
        et3.setText(df.format(Total));
    }
    if (i==rb2.getId()){
        double Bill = Double.parseDouble(et1.getText().toString());
        double Tip = Bill * .1;
        double Total = Tip + Bill;
        DecimalFormat df = new DecimalFormat("#.00");
        et2.setText(df.format(Tip));
        et3.setText(df.format(Total));
    }
    if (i==rb3.getId()){
        double Bill = Double.parseDouble(et1.getText().toString());
        double Tip = Bill * .15;
        double Total = Tip + Bill;
        DecimalFormat df = new DecimalFormat("#.00");
        et2.setText(df.format(Tip));
        et3.setText(df.format(Total));
    }
    if (i==rb4.getId()){
        double Bill = Double.parseDouble(et1.getText().toString());
        double Tip = Bill * .2;
        double Total = Tip + Bill;
        DecimalFormat df = new DecimalFormat("#.00");
        et2.setText(df.format(Tip));
        et3.setText(df.format(Total));
    }
}
public void myClickHandler(View view)
{
    switch (view.getId())
    {
    case R.id.calcBtn:
        break;

    case R.id.resetBtn:
        et1.setText("");
        et2.setText("");
        et3.setText("");
        break;
    }
}

}

xmlファイル

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/totalBill" />

<EditText
    android:id="@+id/billText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal" >
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="22dp"
    android:text="@string/tip" />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center"
    android:id="@+id/radioGroup1"
     >

<RadioButton
    android:id="@+id/five"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:text="@string/perefive" />

<RadioButton
    android:id="@+id/ten"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:text="@string/pereten" />

<RadioButton
    android:id="@+id/fifteen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:text="@string/perefifteen" />

<RadioButton
    android:id="@+id/twenty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="@string/peretwenty" />

</RadioGroup>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="17dp"
    android:text="@string/tiptotal" />

<EditText
    android:id="@+id/tipText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="@string/overalltotal" />

<EditText
    android:id="@+id/totalText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/calcBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:onClick="myClickHandler"
    android:text="@string/calc" />

<Button
    android:id="@+id/resetBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:onClick="myClickHandler"
    android:text="@string/reset" />
    </LinearLayout>
4

2 に答える 2

1

View.OnClickListenerButtonが機能するようにMainActivityクラスに実装 します。ラジオボタンを選択し、[計算]をクリックしてチップを計算し、コードを次のように変更します。

public class MainActivity extends Activity 
     implements RadioGroup.OnCheckedChangeListener, View.OnClickListener{

EditText et1;
EditText et2;
EditText et3;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
RadioGroup rg1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et1=(EditText)findViewById(R.id.billText);
    et2=(EditText)findViewById(R.id.tipText);
    et3=(EditText)findViewById(R.id.totalText);
    rb1=(RadioButton)findViewById(R.id.five);
    rb2=(RadioButton)findViewById(R.id.ten);
    rb3=(RadioButton)findViewById(R.id.fifteen);
    rb4=(RadioButton)findViewById(R.id.twenty);
    rg1=(RadioGroup)findViewById(R.id.radioGroup1);
    rg1.setOnCheckedChangeListener(this);

}
public void myClickHandler(View view)
{

    switch (view.getId())
    {
    case R.id.calcBtn:

      int i =   rg1.getCheckedRadioButtonId();
      if (i==rb1.getId()){
        double Bill = Double.parseDouble(et1.getText().toString());
        double Tip = Bill * .05;
        double Total = Tip + Bill;
        DecimalFormat df = new DecimalFormat("#.00");
        et2.setText(df.format(Tip));
        et3.setText(df.format(Total));
      }
     if (i==rb2.getId()){
        double Bill = Double.parseDouble(et1.getText().toString());
        double Tip = Bill * .1;
        double Total = Tip + Bill;
        DecimalFormat df = new DecimalFormat("#.00");
        et2.setText(df.format(Tip));
        et3.setText(df.format(Total));
    }
    if (i==rb3.getId()){
        double Bill = Double.parseDouble(et1.getText().toString());
        double Tip = Bill * .15;
        double Total = Tip + Bill;
        DecimalFormat df = new DecimalFormat("#.00");
        et2.setText(df.format(Tip));
        et3.setText(df.format(Total));
    }
    if (i==rb4.getId()){
        double Bill = Double.parseDouble(et1.getText().toString());
        double Tip = Bill * .2;
        double Total = Tip + Bill;
        DecimalFormat df = new DecimalFormat("#.00");
        et2.setText(df.format(Tip));
        et3.setText(df.format(Total));
       break;
    }
    case R.id.resetBtn:
        et1.setText("");
        et2.setText("");
        et3.setText("");
        break;
    }
}

}
于 2012-12-06T03:05:54.640 に答える
0

次に、ラジオボタンのonCheckChangedの計算ロジックを削除してみてください。

public void myClickHandler(View view)
{
    switch (view.getId())
    {
    case R.id.calcBtn:


    if(rb1.isChecked()){
    double Bill = Double.parseDouble(et1.getText().toString());
            double Tip = Bill * .05;
            double Total = Tip + Bill;
            DecimalFormat df = new DecimalFormat("#.00");
            et2.setText(df.format(Tip));
            et3.setText(df.format(Total));
    }
    if(rb2.isChecked()){
    // same as you did in your logic
    }
    if(rb3.isChecked()){
    // same as you did in your logic
    }
    if(rb4.isChecked()){
    // same as you did in your logic
    }
break;
 case R.id.resetBtn:
        et1.setText("");
        et2.setText("");
        et3.setText("");
        break;
    }
}
于 2012-12-06T03:15:04.820 に答える