Intent跳转

一、Intent分为两类:

      (1)显示Intent,通过名字指定目标组件。显式的intent用于程序内部消息——如:Activity启动一个下属服务或启动一个姊妹Activity。

     (2)隐式的Intent,没有指定目标,Android系统需要查找最适合处理intent的组件(或几个组件)——一个单一的Activity。通过把intent对象的内容和intent管理器比较,判断那个组件是潜在的接收者。过滤器提供组件的能力并且划定它可以处理的intent。它开启可以接收隐式intent的组件类型。如果组件没有intent过滤器,它仅仅可以接收显式的intent。含有过滤器的组件既可以接收隐式intent也可以接收显式intent。

二、传递数据

  (1) 通过Bundle打包批量传数据

  Bundle bundle=new Bundle();
  bundle.putString("name", name);
  bundle.putInt("age", 16);
  intent.putExtras(bundle

    (2)使用intent.putExtra("key", value)一个个传输数据。

package com.example.sayhellotoyou;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText etName;
	private static final String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName=(EditText)findViewById(R.id.etName);
     
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    public void sayHello(View view){
    	
    	String name = etName.getText().toString();
		Intent intent =new Intent();
		/*intent.setAction(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel://5556"));*/
		/*intent.setClass(this, ShowActivity.class);*/<pre class="html" name="code">package com.example.sayhellotoyou;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ShowActivity extends Activity {
	private TextView tvName;
	private String name;
	private int age;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);
		getData();
		initViews();
		bindData();
	}

	private void bindData() {
		// TODO Auto-generated method stub
		tvName.setText("名字是"+name+"年龄是"+age+"。");
		
	}

	private void initViews() {
		// TODO Auto-generated method stub
		 tvName =(TextView)findViewById(R.id.tvName);
		
	}

	private void getData() {
		// TODO Auto-generated method stub
		/*Bundle bundle=getIntent().getExtras();
		name=bundle.getString("name");
		age=bundle.getInt("age");*/
		 name=getIntent().getStringExtra("name");
		 age=getIntent().getIntExtra("age",12);
	}

}
 


 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值