你好我是新來的android編程。我想問如何使用intent將數據傳遞給另一個活動?我的情況是,我有3個單選按鈕,如果用戶單擊第1個按鈕並最終單擊確定按鈕,它應該被檢索(文本)到其他活動。安卓數據使用意圖傳遞給另一個活動
0選項1 0選項2 0選項3
然後在其他活動應該是這樣的:1選擇 選項。
你好我是新來的android編程。我想問如何使用intent將數據傳遞給另一個活動?我的情況是,我有3個單選按鈕,如果用戶單擊第1個按鈕並最終單擊確定按鈕,它應該被檢索(文本)到其他活動。安卓數據使用意圖傳遞給另一個活動
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);
}
}
};
和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
}
嗨,我已經把它放在OnClick,但我的應用程序崩潰。 – 2012-07-11 16:34:08
看到我的編輯答案,或者如果應用程序仍然崩潰,然後發佈您的代碼 – 2012-07-11 16:36:12
我在strtext = RadioButton1.getText(); – 2012-07-11 17:08:32
在你的第一個活動使用這樣的事情:
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");
結帳This- http://www.androidaspect.com/2012/07/passing-data-using-intent-object.html – 2012-07-11 16:57:49