android---Intent的使用

1、打开新的Activity,不传递参数

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(MainActivity.this, HelloActivity.class);//HellActivity为新的Activity
				startActivity(intent);
			}
		});
    }

2、打开新的Activity并传递参数,用Bundle传值:

MainActivity.java:

package com.example.intent;

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

public class MainActivity extends Activity {
	private Button button;
	private EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到Button
        button = (Button) findViewById(R.id.button);
        //找到Text
        editText = (EditText) findViewById(R.id.text);
        OnClickListener onClickListener = new OnClickListener() {
			public void onClick(View v) {
				//取得EditText的值
		        String text = editText.getText().toString();
				//新建一个Bundle
		        Bundle bundle = new Bundle();
		        //赋值
		        bundle.putString("text", text);
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, HelloActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		};
		button.setOnClickListener(onClickListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

在HelloActivity.java中接收参数

package com.example.intent;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class HelloActivity extends Activity {
	private TextView textView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);
        textView = (TextView) findViewById(R.id.textview);
        //得到当前的Intent
        Intent intent = this.getIntent();
        //得到Bundle
        Bundle bundle = intent.getExtras();
        //通过key得到value
        String text = bundle.getString("text");
        //赋值给textview
        textView.setText(text);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_hello, menu);
        return true;
    }
}

传参的第二种方式:

MainActivity.java:

package com.example.intent;

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

public class MainActivity extends Activity {
	private Button button;
	private EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到Button
        button = (Button) findViewById(R.id.button);
        //找到Text
        editText = (EditText) findViewById(R.id.text);
        OnClickListener onClickListener = new OnClickListener() {
			public void onClick(View v) {
				//取得EditText的值
		        String text = editText.getText().toString();
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, HelloActivity.class);
				intent.putExtra("text",text);
				startActivity(intent);
			}
		};
		button.setOnClickListener(onClickListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

在HelloActivity.java中接收参数

package com.example.intent;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class HelloActivity extends Activity {
	private TextView textView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);
        textView = (TextView) findViewById(R.id.textview);
        //得到当前的Intent
        Intent intent = this.getIntent();
        //通过key得到value
        String text = intent.getStringExtra("text");
        //赋值给textview
        textView.setText(text);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_hello, menu);
        return true;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值