我是Android的新手,并试图为朋友制作一款小应用。在这个应用程序中有一个人kan填写和发送的表单。它包含有关姓名和地址等的信息。 表单完美无缺,但我无法找到如何将值从已检查的单选按钮传输到表单。
package com.example.x_pand;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Bestilling extends Activity implements RadioGroup.OnCheckedChangeListener {
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bestilling);
rg = (RadioGroup) findViewById(R.id.radio_group1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton r1 = (RadioButton) findViewById(R.id.radioButton1);
if(r1.isChecked())
{
Toast.makeText(getBaseContext(), "Afhentningsdag bliver næst kommende onsdag", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getBaseContext(), "Afhentningsdag bliver næst kommende fredag", Toast.LENGTH_LONG).show();
}
});
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText addy = (EditText) findViewById(R.id.editText2);
final EditText city = (EditText) findViewById(R.id.editText3);
final EditText emaile = (EditText) findViewById(R.id.editText6);
final EditText post = (EditText) findViewById(R.id.editText5);
final EditText tlf = (EditText) findViewById(R.id.editText7);
Button mail = (Button) findViewById(R.id.send_form);
mail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
//TODO Auto-generated method stub
Intent email = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
email.setType("plain/text");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"test@mydomain.dk"});
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Bestilling til x-pand");
email.putExtra(android.content.Intent.EXTRA_TEXT,
"Navn: "+name.getText().toString()+'\n'+"Adresse: "+addy.getText().toString()+'\n'+"By: "+city.getText().toString()+ '\n'+"E-mail: "+emaile.getText().toString()+'\n'+"Tlf: "+tlf.getText().toString()+'\n'+"Post nr.: "+post.getText().toString()+'\n'+'\n'+"Bestilling til x-pand");
/* Send it off to the Activity-Chooser */
startActivity(Intent.createChooser(email, "Send mail..."));
}
});
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
这样做..
// Gets a reference to our radio group
// rBtnDigits is the name of our radio group (code not shown)
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);
// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();
// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);
// Now you can get the text or whatever you want from the "selected" radio button
b.getText();
答案 1 :(得分:0)
这样做
RadioGroup rg = (RadioGroup) findViewById(R.id.radio_group1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton r1 = (RadioButton) group.findViewById(checkedId); //UPDATE HERE
if (r1.isChecked())
{
Toast.makeText(getBaseContext(),
"Afhentningsdag bliver næst kommende onsdag",
Toast.LENGTH_LONG).show();
} else
Toast.makeText(getBaseContext(),
"Afhentningsdag bliver næst kommende fredag",
Toast.LENGTH_LONG).show();
}
});