Intent在实现Activity之间的跳转时,可以传递值。下面是一个简单是实例:
首先创建两个xml文件:
activity_main.xml:
<span style="font-family:SimSun;font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击传值!" />
</RelativeLayout>
</span>
activity_second.xml:
<span style="font-family:SimSun;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/name"
android:layout_alignBottom="@+id/name"
android:layout_alignParentLeft="true"
android:layout_marginLeft="35dp"
android:background="#123456"
android:text="number"
android:textSize="45px" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="35dp"
android:layout_marginTop="50dp"
android:background="#345678"
android:text="name"
android:textSize="45px" />
</RelativeLayout>
</span>
然后写java文件
MainActivity
<span style="font-family:SimSun;font-size:14px;">package com.mytext1.mytext1;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.widget.TextView;
import android.widget.TextClock;
public class MainActivity extends Activity {
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.textview);
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Textclass text=new Textclass();
text.setNumber(201314);
text.setName("夏洛");
Bundle bundle=new Bundle();
bundle.putSerializable("text", text);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
</span>
然后是SecondActivity:
<span style="font-family:SimSun;font-size:14px;">package com.mytext1.mytext1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity{
private Textclass text;
private TextView number;
private TextView name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent=getIntent();
text=(Textclass)intent.getSerializableExtra("text");
number=(TextView)findViewById(R.id.number);
name=(TextView)findViewById(R.id.name);
number.setText(String.valueOf(text.getNumber()));
name.setText(text.getName());
}
}
</span>
然后是自定义类Textclass:
<span style="font-family:SimSun;font-size:14px;">package com.mytext1.mytext1;
import java.io.Serializable;
import android.R.string;
public class Textclass implements Serializable {
private String name;
private int number;
public String getName() {
return name;
}
public void setName(String string) {
this.name = string;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
</span>
最后在AndriodManifest里申明一下SecondActivity就好了。
这就实现了Intent跳转时值的传递。