Android - 注册按钮单击并根据无线电选择采取行动

MaQ*_*eod 6 android button radio-button

我正在尝试自学如何编写Android应用程序,但我无法注册按钮点击并根据当时选择的单选按钮采取措施.这是一个简单的小费计算器:

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.view.View;

public class TipCalc extends Activity implements RadioGroup.OnCheckedChangeListener,View.OnClickListener
{
    TextView result;
    RadioGroup radiogroup1;
    RadioButton r1,r2,r3;
    Button calculate;
    EditText bill, resulttotal;
    private int radioCheckedId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        radiogroup1 = (RadioGroup) findViewById(R.id.radiogroup1);
        Button calculate = (Button) findViewById(R.id.calculate); 
        RadioButton r1 = (RadioButton) findViewById(R.id.poor);
        RadioButton r2 = (RadioButton) findViewById(R.id.average);
        RadioButton r3 = (RadioButton) findViewById(R.id.excellent);
        EditText bill = new EditText(this);
        EditText resulttotal = new EditText(this);
        radiogroup1.setOnCheckedChangeListener(this);
        calculate.setOnClickListener(this); 
        //bill.setText("0");
        //resulttotal.setText("0");
     }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        radioCheckedId = checkedId;
    }

    public void onClick(View v)
        {
            if (v == calculate)
           {
                String billtotal;
                double total = 0;
                billtotal = bill.getText().toString();
                final int aInt = Integer.parseInt(billtotal);
                if (radioCheckedId == 1)
                {
                    total = aInt * 1.1;
                    final String aString = Double.toString(total);
                    resulttotal.setText(aString);
                }
               if (radioCheckedId == 2)
                {
                    total = aInt * 1.15;
                    final String aString = Double.toString(total);
                    resulttotal.setText(aString);
                }
               if (radioCheckedId == 3)
               {
                    total = aInt * 1.2;
                    final String aString = Double.toString(total);
                    resulttotal.setText(aString);
               }
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

一切都加载得很好,但是当我按下虚拟手机中的计算按钮时没有任何反应.

Jer*_*gan 10

问题是你要比较RadioGroup的选定ID ...你想要将你的onClick()更改为:

public void onClick(View v) {
    if (v == calculate) {
        String billtotal;
        double total = 0;
        billtotal = bill.getText().toString();
        final int aInt = Integer.parseInt(billtotal);
        if (radioCheckedId == R.id.poor) {
            total = aInt * 1.1;
            final String aString = Double.toString(total);
            resulttotal.setText(aString);
        }
        if (radioCheckedId == R.id.average) {
            total = aInt * 1.15;
            final String aString = Double.toString(total);
            resulttotal.setText(aString);
        }
        if (radioCheckedId == R.id.excellent) {
            total = aInt * 1.2;
            final String aString = Double.toString(total);
            resulttotal.setText(aString);
        }
    }
}    
Run Code Online (Sandbox Code Playgroud)

onCheckedChanged()给出了视图的R.id,而不仅仅是一个数字,告诉你它是按顺序排列的.

一些快速(无关)的建议:

  • 使用switch语句而不是一堆if语句.
  • 把东西放在那里检查-1(没有检查)...只是为了确定.
  • 在onClick()中,我通常通过检查传入视图的id来检查单击了哪个View.这只是让你无需保存所有内容的地方,而且(恕我直言)会更清楚你所说的内容.

以上建议看起来像:

public void onClick(View v) {
    if (v.getId() == R.id.calculate) {
        String billtotal;
        double total = 0;
        billtotal = bill.getText().toString();
        final int aInt = Integer.parseInt(billtotal);
        switch(radioCheckedId) {
            case R.id.poor:
                total = aInt * 1.1;
                final String aString = Double.toString(total);
                resulttotal.setText(aString);
                break;
            case R.id.average:
                total = aInt * 1.15;
                final String aString = Double.toString(total);
                resulttotal.setText(aString);
                break;
            case R.id.excellent:
                total = aInt * 1.2;
                final String aString = Double.toString(total);
                resulttotal.setText(aString);
                break;
            default:
                // do something for when nothing is selected... maybe throw an error?
                break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,如果您在onCheckedChanged()中所做的一切都是存储值,您可以将它们全部放在一起,并在onClick()中检查它.就像是:

public void onClick(View v) {
    int radioCheckedId = radiogroup1.getCheckedRadioButtonId();
    if (v == calculate) {
        // ...
Run Code Online (Sandbox Code Playgroud)

不相关,但我注意到的另一个问题(和其他人提到的)...如果您的EditText在XML布局中列出,那么您需要像这样挂钩(而不是创建新的):

EditText bill        = (EditText) findViewById(R.id.bill       );
EditText resulttotal = (EditText) findViewById(R.id.resulttotal);
Run Code Online (Sandbox Code Playgroud)

此外,如果您不需要它可编辑,您可能只需使用TextView而不是EditView作为结果.


MaQ*_*eod 6

import java.text.NumberFormat;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.view.View;

public class TipCalc extends Activity
{
    TextView result;
    RadioGroup radiogroup1;
    RadioButton r1,r2,r3;
    Button calculate;
    EditText bill, resulttotal;
    Locale currentLocale = Locale.getDefault();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        radiogroup1 = (RadioGroup) findViewById(R.id.radiogroup1);
        final Button calculate = (Button) findViewById(R.id.calculate); 
        final RadioButton r1 = (RadioButton) findViewById(R.id.poor);
        final RadioButton r2 = (RadioButton) findViewById(R.id.average);
        final RadioButton r3 = (RadioButton) findViewById(R.id.excellent);
        final EditText bill = (EditText) findViewById(R.id.bill);
        final EditText tiptotal = (EditText) findViewById(R.id.tiptotal);
        final EditText resulttotal = (EditText) findViewById(R.id.resulttotal);
        bill.setText("0.00");
        tiptotal.setText("0.00");
        resulttotal.setText("0.00");
        calculate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) throws  NumberFormatException {
                if (v == calculate)
                {
                NumberFormat currencyFormatter;
                currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
                double atotal = 0;
                    double btotal = 0;
                    String billtotal = bill.getText().toString();
                    Double aDbl = 0.00;
                    try
                    {
                        aDbl = Double.parseDouble(billtotal);
                    }
                    catch(NumberFormatException n)
                    {
                        aDbl = 0.00;
                    }
                    if (r1.isChecked())
                     {
                        atotal = aDbl * 1.1;
                        btotal = aDbl * 0.1;
                     }
                    if (r2.isChecked())
                     {
                        atotal = aDbl * 1.15;
                        btotal = aDbl * 0.15;
                    }
                    if (r3.isChecked())
                    {
                        atotal = aDbl * 1.2;
                        btotal = aDbl * 0.2;
                    }
                    final String bString = currencyFormatter.format(btotal);
                    tiptotal.setText(bString);
                    final String aString = currencyFormatter.format(atotal);
                    resulttotal.setText(aString);
                 }
            }
        });

     }
}
Run Code Online (Sandbox Code Playgroud)