2012-07-11 65 views
1

你好我是新来的android编程。我想问如何使用intent将数据传递给另一个活动?我的情况是,我有3个单选按钮,如果用户单击第1个按钮并最终单击确定按钮,它应该被检索(文本)到其他活动。安卓数据使用意图传递给另一个活动

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

然后在其他活动应该是这样的:1选择 选项。

+0

结帐This- http://www.androidaspect.com/2012/07/passing-data-using-intent-object.html – 2012-07-11 16:57:49

回答

3

您可以通过为两项活动之间的数据:

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

和Receiveractivity:

//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 
    } 
+0

嗨,我已经把它放在OnClick,但我的应用程序崩溃。 – 2012-07-11 16:34:08

+0

看到我的编辑答案,或者如果应用程序仍然崩溃,然后发布您的代码 – 2012-07-11 16:36:12

+0

我在strtext = RadioButton1.getText(); – 2012-07-11 17:08:32

2

在你的第一个活动使用这样的事情:

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()活动,用它来检索选定的RadioButtons文本:

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