MainActivity.java 의 파일 내용
package com.example.androidexam; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnTest1 = (Button)findViewById(R.id.btnTest1); btnTest1.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.btnTest1: Intent intent = new Intent(getBaseContext(), SubActivity.class); intent.putExtra("name", "홍길동"); intent.putExtra("age", 29); startActivity(intent); break; case R.id.btnTest2: Toast.makeText(getBaseContext(), "btnTest2 클릭", Toast.LENGTH_SHORT).show(); break; default: break; } } }
SubActivity.java 의 파일 내용
package com.example.androidexam; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class SubActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); Intent intent = new Intent(this.getIntent()); String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age", 1); Toast.makeText(getBaseContext(), name + "님의 나이는 " + age + "입니다.", Toast.LENGTH_SHORT).show(); } }
댓글 없음 :
댓글 쓰기