使用intent将android数据传递给另一个活动

使用intent将android数据传递给另一个活动,android,Android,你好,我是android编程新手。我想问一下,如何使用intent将数据传递给另一个活动?我的例子是,我有3个单选按钮,如果用户单击第一个按钮,最后单击OK按钮,它应该被检索到其他活动(文本) 0选项1 0选项2 0选项3 那么在其他活动中,应该是这样的: 选择选项1。您可以在两个活动之间作为数据传递: calculate = (Button) findViewById(R.id.calculateButton); private OnClickListener calculateButtonL

你好,我是android编程新手。我想问一下,如何使用intent将数据传递给另一个活动?我的例子是,我有3个单选按钮,如果用户单击第一个按钮,最后单击OK按钮,它应该被检索到其他活动(文本)

0选项1 0选项2 0选项3

那么在其他活动中,应该是这样的:
选择选项1。

您可以在两个活动之间作为数据传递:

calculate = (Button) findViewById(R.id.calculateButton);
private OnClickListener calculateButtonListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        String strtext="";
             if(RadioButton1.isChecked())
             {
              strtext=RadioButton1.getText();
             }
             if(RadioButton2.isChecked())
             {
             strtext=RadioButton1.getText();
             }
             if(RadioButton1.isChecked())
             {
              strtext=RadioButton3.getText();
             }
             if(!strtext.equals(""))
             {
               //Create new Intent Object, and specify class
                Intent intent = new Intent();  
                intent.setClass(SenderActivity.this,Receiveractivity.class);

                //Set your data using putExtra method which take 
                //any key and value which we want to send 
                intent.putExtra("senddata",strtext);  

                //Use startActivity or startActivityForResult for Starting New Activity
                SenderActivity.this.startActivity(intent); 
             }
        }
    };
在接管活动中:

//obtain  Intent Object send  from SenderActivity
  Intent intent = this.getIntent();

  /* Obtain String from Intent  */
  if(intent !=null)
  {
     String strdata = intent.getExtras().getString("senddata");
    // DO SOMETHING HERE
  }
  else
  {
    // DO SOMETHING HERE
  }

在您的第一个活动中,请使用以下内容:

okButton.setOnClickListener(new OnClickListener() {
    public onClick(View view) {
        RadioButton selected = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());

        Intent intent = new Intent(First.this, Second.class);
        intent.putExtra("Radio Choice", selected.getText().toString());
        startActivity(intent);
    }
});
在第二个.onCreate()活动中,使用此选项检索所选单选按钮的文本:

Bundle extras = getIntent().getExtras();
if(extras != null)
    String choice = extras.getString("Radio Choice");

嗨,我已经把它放在OnClick中了,但是我的应用程序崩溃了。请查看我的编辑答案,或者如果应用程序仍在崩溃,请发布您的codeCheckout-