你好,我是 Android 编程新手。我想问一下如何使用 Intent 将数据传递给另一个 Activity ?我的情况是,我有 3 个单选按钮,如果用户单击第一个按钮并最终单击“确定”按钮,则应该将其检索(文本)到其他 Activity 。
0 选项 1 0 选项 2 0 选项 3
然后在其他 Activity 中应该是这样的: 选择选项 1。
最佳答案
您可以在两个 Activity 之间传递数据:
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);
}
}
};
在接收 Activity 中:
//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
}
关于android数据使用 Intent 传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437081/