Android - 注册按钮点击并根据单选选择采取行动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1360352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - register button click and take action based on radio selection
提问by MaQleod
I'm trying to teach myself how to write android apps and I'm having trouble registering a button click and taking actions based on which radio button is selected at the time. This is a simple tip calculator:
我正在尝试自学如何编写 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);
}
}
}
}
Everything loads just fine, but nothing happens when I press the calculate button in the virtual phone.
一切都加载得很好,但是当我按下虚拟电话中的计算按钮时没有任何反应。
回答by Jeremy Logan
The problem is where you're comparing the RadioGroup's selected id... you'll want to change your onClick() to:
问题是您在比较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);
}
}
}
onCheckedChanged()gives you will be the R.id for the view and not just a number which tells you which it is in sequence.
onCheckedChanged()为您提供视图的 R.id,而不仅仅是告诉您按顺序排列的数字。
A few quick (unrelated) suggestions:
一些快速(不相关)的建议:
- Use a switch statement instead of a bunch of if-statements.
- Put something in there to check for -1 (nothing checked) too... just to be sure.
- In the onClick() I usually check for which View was clicked by checking the incoming view's id. This just makes it where you don't have to keep everything stored and (IMHO) is a little more clear what you're talking about.
- 使用 switch 语句而不是一堆 if 语句。
- 在那里放一些东西来检查 -1(没有检查)......只是为了确定。
- 在 onClick() 中,我通常通过检查传入视图的 id 来检查单击了哪个视图。这只是使您不必保存所有内容,并且(恕我直言)更清楚您在说什么。
The above suggestions would look something like:
上面的建议看起来像:
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;
}
}
}
Lastly, if all you're doing in onCheckedChanged() is storing the value you could get rid of it all together and just check for it in the onClick(). Something like:
最后,如果您在 onCheckedChanged() 中所做的只是存储值,您可以将其全部删除,只需在 onClick() 中检查它。就像是:
public void onClick(View v) {
int radioCheckedId = radiogroup1.getCheckedRadioButtonId();
if (v == calculate) {
// ...
Unrelated, but another problem I noticed (and someone else mentioned)... if your EditTexts are listed in the XML layout then you'd need to get hooks to them like this (and not create new ones):
不相关,但我注意到的另一个问题(和其他人提到的)...如果您的EditText列在 XML 布局中,那么您需要像这样获取它们的钩子(而不是创建新的钩子):
EditText bill = (EditText) findViewById(R.id.bill );
EditText resulttotal = (EditText) findViewById(R.id.resulttotal);
Also, you could probably just use a TextViewinstead of an EditView for the result if yo udon't need it to be editable.
此外,如果您不需要它可编辑,您可能只使用TextView而不是 EditView 作为结果。
回答by MaQleod
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);
}
}
});
}
}
回答by Rui
I have some similar problem. I have a countdown in a radio group activity. When user clicks the next Button the radio group is checked to see if an option is selected. I implemented the button pressed at the end of the countdown, now i need to pass a checked radio Id to bypass the default user message of an option not selected.
我有一些类似的问题。我有一个广播小组活动的倒计时。当用户单击下一个按钮时,检查单选组以查看是否选择了选项。我实现了在倒计时结束时按下的按钮,现在我需要传递一个选中的无线电 Id 来绕过未选中选项的默认用户消息。
case R.id.next:
案例R.id.next:
Log.d(" ID BOTAO",((java.lang.String) String).valueOf(rGroup3.getCheckedRadioButtonId()));
if(rGroup3.getCheckedRadioButtonId()==-1){
Context context = getApplicationContext();
CharSequence text = "Please, select an option!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
break;
}
回答by Lucas S.
Your problem is that you never add EditText instances to the current layout.
您的问题是您从未将 EditText 实例添加到当前布局。
You should add them as children of the main layout.
您应该将它们添加为主布局的子项。