在本讲中,小编将想大家介绍Android中最常用的传值方式:Handler和Intent
Intent是为了实现Activity之间跳转的工具,它同样可以进一步将某些数据传递到终点Activity。当然Intent的传值是无法代替SharedPreferences的。下面,便通过代码向大家介绍Intent功能的实现。具体的详细解释,小编已写入代码中。
本次一共创建了三个Activity。其中TextActivity展示了普通的Intent跳转,ExtraActivity展示了Intent如何传递数据。
package com.example.intentdemo;
/**
* @author Arthur Lee
* @time 05/26/2014
* */
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText et,send,receive;
private Button bt1,bt2,bt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.intent_edit);
send = (EditText)findViewById(R.id.intent_send);
receive = (EditText)findViewById(R.id.intent_receive);
bt1 = (Button)findViewById(R.id.intent_bt1);
bt2 = (Button)findViewById(R.id.intent_bt2);
bt3 = (Button)findViewById(R.id.intent_bt3);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/**
* Activity的切换一般是通过Intent来实现的,
* Intent是一个Activity到达另一个Activity的桥接者,
* 它描述了起点(当前Activity)和终点(目标Activity)。
* 一个简单Intent实现如下:*/
//初始化Intent变量
Intent intent = new Intent();
//描述当前跳转的起点和终点
intent = intent.setClass(MainActivity.this, TextActivity.class);
//启动跳转
startActivity(intent);
/**
* 调用finish()方法,会触发Distory(),即终止了当前Activity,
* 再次跳转到当前页面时将会是新的一页,若想保存相关页面信息,可使用SharedParenfences。
* 详细介绍请关注第七讲*/
finish();
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-ge