页面跳转Sample
案例演示了Intent之间的数据传递, 其中包含三个Activity:
- MainActivity
- 按钮1, 从ContactActivity中获取电话号码数据,结束并返回和显示数据
- 按钮2, 从SmsTemplateActivity中获取文本数据,结束并返回和显示数据
- ContactActivity
- 一个ListView,其中包含电话号码数据
- SmsTemplateActivity
- 一个ListView,其中包含短信数据
使用方法:
- startActivityForResult()
- 重写onActivityResult()
- setResult(requestCode, Intent)
布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.goldinfo.mymsgsender.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入发送的号码" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/et_number"
android:onClick="add"
android:text="+" />
</RelativeLayout>
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="top"
android:hint="请输入短信的内容" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="插入模板"
android:onClick="insert"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送"
android:onClick="send"
/>
</LinearLayout>
activity_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
contact_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#109384"
android:text="SunShine" />
<TextView
android:id="@+id/tv_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#990090"
android:text="111"
android:textSize="20sp" />
</LinearLayout>
activity_smstemplate.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"></ListView>
</LinearLayout>
template_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#990011"
/>
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText et_number;
private EditText et_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
et_number = (EditText) findViewById(R.id.et_number);
et_content = (EditText) findViewById(R.id.et_content);
}
//点击按钮跳转到ContactActivity页面
public void add(View v) {
//创建意图对象
Intent intent = new Intent(this, ContactActivity.class);
//当我们开启一个Activity, 我需要这个新的Activity的数据, 应该使用下面这个方法启动
startActivityForResult(intent, 10);
}
//点击按钮跳转到短信模板页面
public void insert(View v) {
Intent intent = new Intent(this, SmsTemplateActivity.class);
startActivityForResult(intent, 20);
}
//这个方法当startActivityForResult开启的Activity的页面关闭的时候就调用这个方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
//说明结果数据来源于 联系人页面
//参数data就是ContactActivity传递过来的意图对象
String number = data.getStringExtra("number");
//把phone设置到EditText上
et_number.setText(number);
} else if (resultCode == 20) {
//说明结果数据来源于模板
String smsContent = data.getStringExtra("smscontent");
et_content.setText(smsContent);
}
}
}
Contact.java
public class Contact {
private String name;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String number;
}
ContactActivity.java
public class ContactActivity extends AppCompatActivity {
private ListView lv;
private List<Contact> lists;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载布局
setContentView(R.layout.activity_contact);
//找到ListView控件
lv = (ListView) findViewById(R.id.lv);
//准备ListView要显示的数据
lists = new ArrayList<Contact>();
for(int i=0; i<20; i++){
Contact contact = new Contact();
contact.setName("Sun"+i);
contact.setNumber("01888"+i);
lists.add(contact);
}
lv.setAdapter(new MyAdapter());
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//取出点中条目的电话号码, 数据在哪里存就去哪里取
String number = lists.get(position).getNumber();
//把电话号码number返回给MainActivity
Intent intent = new Intent();
intent.putExtra("number", number);
//把我要传递的数据传递回给MainActivity
setResult(10, intent);
finish();
}
});
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
//历史缓存对象为空
view = View.inflate(getApplicationContext(), R.layout.contact_item, null);
}else{
view = convertView;
}
//找到contact_item 这个布局里面的控件 用来显示数据
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
//显示数据
tv_name.setText(lists.get(position).getName());
tv_number.setText(lists.get(position).getNumber());
return view;
}
}
}
SmsTemplate.java
public class SmsTemplateActivity extends AppCompatActivity {
private ListView lv;
String[] greetings = {"Good Morning!", "Good Afternoon!", "Hello!", "你好!", "Good Night!", "Good Evening!",
"Bonjour!"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smstemplate);
//找到控件listView
lv = (ListView) findViewById(R.id.lv);
//数据适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.template_item, greetings);
//显示数据
lv.setAdapter(adapter);
//设置点击事件
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//获取点击条目的数据
String smsContent = greetings[position];
//创建意图
Intent intent = new Intent();
intent.putExtra("smscontent", smsContent);
//把数据返回给调用者
setResult(20, intent);
//把当前页面关闭
finish();
}
});
}
}