Android 我想根据选中的radiobutton传递意图值,只有在用户单击按钮后,这里才是我的代码

Android 我想根据选中的radiobutton传递意图值,只有在用户单击按钮后,这里才是我的代码,android,android-activity,onclicklistener,android-radiobutton,Android,Android Activity,Onclicklistener,Android Radiobutton,如何根据选中的单选按钮传递intent值,intent应仅在单击按钮后启动,并且应根据swith case语句传递值这是我的代码请帮助我 package com.example.simplemath; import android.app.Activity; import android.content.Intent; import android.graphics.Typeface; import android.os.Bundle; import android.view.View; im

如何根据选中的单选按钮传递intent值,intent应仅在单击按钮后启动,并且应根据swith case语句传递值这是我的代码请帮助我

package com.example.simplemath;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.util.Log;

public class Settings extends Activity {
    OnCheckedChangeListener listner = null;
    OnCheckedChangeListener listner2 = null;
    int level = 0;
    int TotalQues = 0;
    int click = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        Bundle b = getIntent().getExtras();
        click = b.getInt("click");

        Typeface custom_font = Typeface.createFromAsset(getAssets(),
                "fonts/SofadiOne-Regular.ttf");
        Typeface custom_font1 = Typeface.createFromAsset(getAssets(),
                "fonts/belligerent.ttf");

        TextView heading1 = (TextView) findViewById(R.id.textView1);
        heading1.setTypeface(custom_font);
        TextView heading2 = (TextView) findViewById(R.id.textView2);
        heading2.setTypeface(custom_font);

        RadioGroup difficulty = (RadioGroup) findViewById(R.id.radioGroup1);

        difficulty.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (group.getCheckedRadioButtonId()) {
                case R.id.radioButton1:
                    Log.d("info", "one");
                    Intent intent1 = new Intent(Settings.this, Third.class);
                    level = 1;
                    intent1.putExtra("level", level);
                    intent1.putExtra("click", click);
                    // startActivity(intent1);
                    break;

                case R.id.radioButton2:
                    Log.d("info", "two");
                    Intent intent2 = new Intent(Settings.this, Third.class);
                    level = 2;
                    intent2.putExtra("level", level);
                    intent2.putExtra("click", click);
                    // startActivity(intent2);
                    break;

                case R.id.radioButton3:
                    Log.d("info", "three");
                    Intent intent3 = new Intent(Settings.this, Third.class);
                    level = 3;
                    intent3.putExtra("level", level);
                    intent3.putExtra("click", click);
                    // startActivity(intent2);
                    break;
                }

            }
        });
        RadioGroup NoOfQuestions = (RadioGroup) findViewById(R.id.radioGroup2);

        NoOfQuestions.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (group.getCheckedRadioButtonId()) {
                case R.id.radioButton4:
                    Log.d("info", "four");
                    Intent intent4 = new Intent(Settings.this, Third.class);
                    Bundle NoTwenty = new Bundle();
                    TotalQues = 1;
                    NoTwenty.putInt("NoQuestions", TotalQues);
                    intent4.putExtras(NoTwenty);
                    // startActivity(intent4);
                    break;

                case R.id.radioButton5:
                    Log.d("info", "five");
                    Intent intent5 = new Intent(Settings.this, Third.class);
                    Bundle NoThirty = new Bundle();
                    TotalQues = 1;
                    NoThirty.putInt("NoQuestions", TotalQues);
                    intent5.putExtras(NoThirty);
                    // startActivity(intent5);
                    break;

                case R.id.radioButton6:
                    Log.d("info", "six");
                    Intent intent6 = new Intent(Settings.this, Third.class);
                    Bundle NoFourty = new Bundle();
                    TotalQues = 1;
                    NoFourty.putInt("NoQuestions", TotalQues);
                    intent6.putExtras(NoFourty);
                    // startActivity(intent6);
                    break;
                }

            }
        });

        RadioButton easy = (RadioButton) findViewById(R.id.radioButton1);
        easy.setTypeface(custom_font1);
        RadioButton medium = (RadioButton) findViewById(R.id.radioButton2);
        medium.setTypeface(custom_font1);
        RadioButton hard = (RadioButton) findViewById(R.id.radioButton3);
        hard.setTypeface(custom_font1);

        RadioButton twenty = (RadioButton) findViewById(R.id.radioButton4);
        twenty.setTypeface(custom_font1);
        RadioButton thirty = (RadioButton) findViewById(R.id.radioButton5);
        thirty.setTypeface(custom_font1);
        RadioButton fourty = (RadioButton) findViewById(R.id.radioButton6);
        fourty.setTypeface(custom_font1);

        Button submit = (Button) findViewById(R.id.button1);
        submit.setTypeface(custom_font);
        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(Settings.this, Third.class));
            }
        });

    }

}

在RadioGroup按钮的OnClickListener中使用以下选项

AlertDialogBuilder voteBuilder = new AlertDialogBuilder(baseContext);

        final Context context = voteBuilder.getThemeContext();

        voteBuilder.setTitle("Vote");

        LayoutInflater inflater = voteBuilder.getLayoutInflater();

        View viewVote = inflater.inflate(R.layout.eip_vote_popup, null, false);

        TextView qsnText = (TextView) viewVote.findViewById(R.id.textPollingQuestion);
        qsnText.setText(description + "?");

        final RadioButton radioPositive = (RadioButton) viewVote.findViewById(R.id.radioPositive);
        final RadioButton radioNegative = (RadioButton) viewVote.findViewById(R.id.radioNegative);

        if (pollType.equals("AD")) {
            radioPositive.setText("Agree");
            radioNegative.setText("Disagree");
        } else {
            radioPositive.setText("Yes");
            radioNegative.setText("No");
        }

        voteBuilder.setView(viewVote);

        voteBuilder.setNegativeButton("Cancel", null);
        voteBuilder.setPositiveButton("Vote", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        voteBuilder.setCancelable(true);

        final AlertDialog dialog = voteBuilder.show();
        dialog.setCanceledOnTouchOutside(true);

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!radioPositive.isChecked() && !radioNegative.isChecked()) {
                    Toast.makeText(context, "Select your opinion", Toast.LENGTH_SHORT).show();
                    return;
                }

                //EIPService service = new EIPService(context);
                //service.SaveUserPoll(pollingId, radioPositive.isChecked());
                dialog.dismiss();
            }
        });

您调用的不是服务,而是必要的意图。不要使用切换情况,有时会出现问题。

将意向实例设置为全局,然后单击按钮将该对象传递给
startActivity(Intent)