【发布时间】:2012-07-11 16:23:51
【问题描述】:
您好,我是 android 编程新手。我想问一下如何使用意图将数据传递给另一个活动?我的情况是,我有 3 个单选按钮,如果用户单击第一个按钮并最终单击“确定”按钮,则应将其检索(文本)到其他活动。
0 选项 1 0 选项 2 0 选项 3
那么在其他活动中应该是这样的: 选择了选项 1。
【问题讨论】:
标签: android
您好,我是 android 编程新手。我想问一下如何使用意图将数据传递给另一个活动?我的情况是,我有 3 个单选按钮,如果用户单击第一个按钮并最终单击“确定”按钮,则应将其检索(文本)到其他活动。
0 选项 1 0 选项 2 0 选项 3
那么在其他活动中应该是这样的: 选择了选项 1。
【问题讨论】:
标签: android
您可以在两个活动之间传递数据:
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);
}
});
在您的 Second.onCreate() Activity 中,使用它来检索所选 RadioButtons 的文本:
Bundle extras = getIntent().getExtras();
if(extras != null)
String choice = extras.getString("Radio Choice");
【讨论】: