安卓开发(6)——安卓页面跳转

一、如何添加新页面

  • src->new->other->Android->Android Activity

二、如何跳转页面

通过在Botton函数中添加onclieck函数实现点击跳转。

public void gotoSecondPage(View v)
{
	//设置跳转的页面
	Intent intent = new Intent(this, SecActivity.class);
	//实行跳转
	startActivity(intent);
}

三、跳转如何传参

  1. 方式一
    在实行跳转页面前进行put数据,在第二个页面中进行get数据。
    public void gotoSecondPage(View v)
    	{
    		//设置跳转的页面
    		Intent intent = new Intent(this, SecActivity.class);
    		intent.putExtra("data", "jiangyo");
    		//实行跳转
    		startActivity(intent);
    	}
    
    private String data;
    	@SuppressLint("ShowToast")
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_sec);
    		
    		Intent i = this.getIntent();
    		data = i.getStringExtra("data");
    		Toast.makeText(this, data, 0).show();
    	}
    
  2. 方式二
    借助bundle类,可实现多种数据类型的传递。
    public void gotoSecondPage(View v)
    	{
    		//设置跳转的页面
    		Intent intent = new Intent(this, SecActivity.class);
    		Bundle bn = new Bundle();
    		bn.putString("data", "jiangyo");
    		bn.putInt("ID", 100);
    		intent.putExtras(bn);
    		//实行跳转
    		startActivity(intent);
    	}
    
    private String data;
    	private int id;
    	
    	@SuppressLint("ShowToast")
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_sec);
    		
    		Intent i = this.getIntent();
    		Bundle bn = i.getExtras();
    		
    		data = bn.getString("data");
    		id = bn.getInt("ID");
    		Toast.makeText(this, data+id, 0).show();
    	}
    

三、页面自动跳转

  • 安卓线程。
  • 实现页面的诺干秒后的自动跳转效果。run方法(函数)是线程要做的”事情”,相当linuxC线程的回调函数启动线程。
    package com.example.jiangyo.learn;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		Thread t = new Thread(new Runnable() {
    			
    			public void run() {
    				// TODO Auto-generated method stub
    				try {
    					Thread.sleep(3000);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				
    				//设置跳转的页面
    				Intent intent = new Intent(MainActivity.this, SecActivity.class);
    				//实行跳转
    				startActivity(intent);
    			}
    		});
    		t.start();
    	}
    }
    

四、Activity(页面)的生命周期

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值