0

我是 Android 新手,正在尝试为朋友制作一个小应用程序。在这个应用程序中,人们可以填写并发送一个表格。它包含有关名称和地址等的信息。表单完美运行,但我无法找到如何将值从选中的单选按钮传输到表单。

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 } }

4

2 回答 2

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();
于 2013-10-03T12:14:51.883 回答
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();
            }

        });
于 2013-10-03T12:15:56.747 回答