当前位置:操作系统 > 安卓/Android >>

android学习笔记11--------------Activity之切换与参数传递

实现功能:

1.两个Activity之间的切换

2.两个Activity带返回值的切换

效果图:

 \

 

点击切换Activity,或带返回值的Activity,出现:

 \

 

点击关闭,其中带返回值的效果如下:

 \

 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
 
public class Main extends Activity 

    Button button; 
    Button button2; 
    TextView textView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        button=(Button)findViewById(R.id.button); 
        button2=(Button)findViewById(R.id.button2); 
        textView=(TextView)findViewById(R.id.display); 
         
        button.setOnClickListener(new OnClickListener() 
        {            
            @Override 
            public void onClick(View v) 
            { 
                /**
                 * 向新activity传参数
                 */ 
                Intent intent= new Intent(Main.this, OtherActivity.class); 
                intent.putExtra("age", 10); 
                intent.putExtra("name", "lingming"); 
                Main.this.startActivity(intent); 
            } 
        }); 
         
        button2.setOnClickListener(new OnClickListener() 
        {            
            @Override 
            public void onClick(View v) 
            { 
                /**
                 * 带返回值的Activity切换
                 */ 
                Intent intent= new Intent(Main.this, OtherActivity.class); 
                intent.putExtra("age", 10); 
                intent.putExtra("name", "lingming"); 
                Main.this.startActivityForResult(intent, 3);//3是标示请求码,随便写  
            } 
        }); 
    } 
    /**
     *  接收带有返回值的Activity方法
     */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
        super.onActivityResult(requestCode, resultCode, data); 
        textView.setText("requestCode:"+requestCode+'\n'+ 
                         "resultCode:"+resultCode+'\n'+ 
                         "接收到:"+data.getStringExtra("返回值") 
        ); 
    } 
     

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity
{
 Button button;
 Button button2;
 TextView textView;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  button=(Button)findViewById(R.id.button);
  button2=(Button)findViewById(R.id.button2);
  textView=(TextView)findViewById(R.id.display);
  
  button.setOnClickListener(new OnClickListener()
  {   
   @Override
   public void onClick(View v)
   {
    /**
     * 向新activity传参数
     */
    Intent intent= new Intent(Main.this, OtherActivity.class);
    intent.putExtra("age", 10);
    intent.putExtra("name", "lingming");
    Main.this.startActivity(intent);
   }
  });
  
  button2.setOnClickListener(new OnClickListener()
  {   
   @Override
   public void onClick(View v)
   {
    /**
  &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,