컴퓨터공부/Android

Intent를 이용한 클래스간 데이터 전달

achivenKakao 2011. 6. 8. 00:51
Intent를 이용하면 클래스간 데이터 전달을 할 수 있다.

참 좋지요~

+

public class DataTransferBetweenClasses extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button)findViewById(R.id.planview);
        btn.setOnClickListener(this);
    }

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.planview:
Intent i = new Intent(this, Plan.class);
Bundle myData = new Bundle();
myData.putString("key", "data");
i.putExtras(myData);
startActivity(i);
break;
}
}
}


 public class Plan extends Activity implements OnClickListener{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datatransferlayout);

        EditText varEditText = (EditText)findViewById(R.id.GetText);
        
        Intent intent0 = getIntent();
        Bundle myBundle = intent0.getExtras();
        varEditText.setText(myBundle.getString("key"));        
    }

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}