一:步骤:
1创建Intent对象,参数中指定将要跳转到的class
2通过Intent对象的putExtra()方法指定要传到下一个页面的数据。方法参数用键值对。
3通过调用startActivity(Intent intent)方法或者startActivityForResult(Intent intent,int requestCode)方法启动跳转。
startActivtyForResult():Activity A跳转到Activity B,Activity B可以返回数据给Activity A。
startActivity():Activity A跳转到 Activity B并可以传值。
4跳转到另一个Activity后,可以通过getIntent().getExtras()方法获得Bundle对象,通过Bundle对象的getString()/getInt()...方法获取到值。
5如果是通过startActivityForResult()跳转的Activity,那么结束了跳转的Activity后还要在被跳转的Activity类中实现onActivityResult()方法.
二:实例
package com.example.apploading; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Lauching extends Activity { private EditText Username=null; private Button btn=null; private OnClickListener mListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i=new Intent(Lauching.this,Exit.class); String s=Username.getText().toString(); i.putExtra("name", s); //startActivityForResult(i,R.layout.activity_exit); startActivity(i); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_loading); Username=(EditText)findViewById(R.id.InputUserName); btn=(Button)findViewById(R.id.Lauching); btn.setOnClickListener(mListener); } }
package com.example.apploading; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Exit extends Activity implements OnClickListener{ Button ExitBtn; Button ReturnBtn; TextView Name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exit); ExitBtn=(Button)findViewById(R.id.ExitBtn); ReturnBtn=(Button)findViewById(R.id.returnBtn); Name=(TextView)findViewById(R.id.Name); String name=getIntent().getExtras().getString("name"); Name.setText(name); ExitBtn.setOnClickListener(this); ReturnBtn.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.exit, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.ExitBtn: finish(); break; case R.id.returnBtn: Intent i=new Intent(this,Lauching.class); startActivity(i); } } }
时间: 2024-10-01 02:21:29