2011년 12월 08일
안드로이드 액티비티(Android Activity) Intent로 이동할 때 값을 넘겨주기
세 번째로 액티비티를 이동할 때 특정 값을 가지고 가는 경우입니다.
세 번째 버튼, 액티비티 이동할 때 값을 넘겨주는 경우. IntentTestMainActivity.java ---> ExtraActivity.java
넘겨주는 값은 "우체국 은행"과 잔액 0원...넘겨주는 메써드는 putExtra("전달명", "넘겨주는 값") ; // 숫자면 "" 없이 그냥 입력.
메인 파일, IntentTestMainActivity.java
package net.br ;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class IntentTestActivity extends Activity {/-* Called when the activity is first created. *-@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);
}
// 값을 넘겨주는 Extra Intentpublic void onClickIntentWidthExtra(View v){Intent intent = new Intent(this, ExtraActivity.class);intent.putExtra("account", "우체국 은행");intent.putExtra("restAmount", 0);this.startActivity(intent);}
}
3. 세 번째 버튼 - 값을 넘겨주는 Activity 전환이동 (ExtraActivity.java) 여기서는 받아온 값을 단순히 출력. 만약 잔고가 0이면 "앵꼬~"라고 출력.넘겨주는 값을 받으려면 문자열은 getStringExtra(), 숫자는 getIntExtra()
package net.br ;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class ExtraActivity extends Activity {@Override
protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.extra);
Intent intent = this.getIntent();String account = intent.getStringExtra("account");int restAmount = intent.getIntExtra("restAmount", 0);
String none = null ;
if(restAmount == 0) {none = "앵꼬~" ;}
TextView nameTextView = (TextView)findViewById(R.id.textView1);
TextView ageTextView = (TextView)findViewById(R.id.textView2);
nameTextView.setText("계좌는 " + account);ageTextView.setText("잔고는 "+ none);}
}
# by | 2011/12/08 14:19 | JavaAndroid | 트랙백
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]