Android学习笔记(五) Acticity跳转并传值

Android的基本活动Activity 之间跳转依靠Intent(意图)实现

 

此时Activity1从创建到跳转 以此经过的状态为onCreate(), onStart(),onResume,onPause(),obDestory();

跳转到Activity2经历的状态为 onCreate(),onStart(),

可以重写这些函数,达到具体想要的效果。

 

Intent i = new Intent(); i.setClass(Activity1.this,Activity2.class); startActivity(i);  

这样就从Activity1跳转到了Activity2

 

如果在跳转的同时需要Activity1向Activity2传递数据

则代码为:

 

 

i.setClass(Activity1.this,Activity2.class); Bundle bundle = new Bundle(); bundle.putString("Data",editText.getText().toString()); i.putExtras(bundle); startActivity(i); 

 

如果需要Activity2返回信息

那么startActivity() 方法改为startActivityForResult()

同时重写onActivityResult();

 

下面实现的例子功能为 

Activity1向Activity2传值,2接受到以后向1发送消息提示收到,并显示传过来的值

 

//ActivityIntent.java

package young.ActivityIntent; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class ActivityIntent extends Activity { public static final String TEST = "test"; private Button btnIntent; private EditText editText; private TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //initView editText = (EditText)findViewById(R.id.editText); tv = (TextView)findViewById(R.id.ac2msg); tv.setText("还没收到Activity2的消息!"); btnIntent = (Button)findViewById(R.id.btnIntent); btnIntent.setOnClickListener(new activity1Listener()); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (resultCode) { case RESULT_OK: Bundle bundle = data.getExtras(); tv.setText(bundle.getString("backData")); break; default: break; } } class activity1Listener implements OnClickListener{ public void onClick(View v) { // TODO Auto-generated method stub if(v==btnIntent){ Intent i = new Intent(); i.setClass(ActivityIntent.this,Activity2.class); Bundle bundle = new Bundle(); bundle.putString("Data",editText.getText().toString()); i.putExtras(bundle); //startActivity(i); startActivityForResult(i, 0); //ActivityIntent.this.finish(); //finish(); } } } } 

 

 

//Activity2.java

 

 

package young.ActivityIntent; import young.ActivityIntent.R.id; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Activity2 extends Activity{ private TextView tv; private Button btnBackToAc1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac2); tv = (TextView)findViewById(R.id.Text); btnBackToAc1 = (Button)findViewById(R.id.btnBacktoAc1); btnBackToAc1.setOnClickListener(new ac2listener()); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Bundle bundle = this.getIntent().getExtras(); tv.setText(bundle.getString("Data")); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); } class ac2listener implements OnClickListener{ public void onClick(View v) { // TODO Auto-generated method stub if(v==btnBackToAc1) { Intent i = new Intent(); i.setClass(Activity2.this, ActivityIntent.class); Bundle bundle = new Bundle(); bundle.putString("backData", "Activity收到信息'"+tv.getText().toString()+"'并返回"); i.putExtras(bundle); setResult(RESULT_OK,i); finish(); } } } }  

 

 

效果图为:

Android学习笔记(五) Acticity跳转并传值_第1张图片

Android学习笔记(五) Acticity跳转并传值_第2张图片Android学习笔记(五) Acticity跳转并传值_第3张图片

你可能感兴趣的:(android,String,活动,Class,button)