7、案例–短信助手(跳转到下一个界面并返回这个界面的数据到原来的界面
如图:
代码体现:
Layout界面代码
第一个是主页面的界面代码:
<LinearLayout 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: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=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_marginTop="15dp"
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:onClick="selectcontact"
android:text="+" />
</RelativeLayout>
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入短信内容"
android:minLines="7" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectsms"
android:text="选择短信模板" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送" />
</LinearLayout>
第2个是短信模板界面和号码选择界面的模板界面的代码:
<?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>
activity界面代码
第一个是主页面界面所对应的activity代码:
package com.itheima.smshelper;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText et_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_content = (EditText) findViewById(R.id.et_content);
/* Intent intent = getIntent();
String content = intent.getStringExtra("content");
et_content.setText(content);*/
}
public void selectsms(View v){
Intent intent = new Intent(this , SmsActivity.class);
// startActivity(intent);
//1. 启动界面,并且等待结果的返回。
startActivityForResult(intent, 1);
}
public void selectcontact(View v){
Intent intent = new Intent(this , ContactActivity.class);
// startActivity(intent);
//1. 启动界面,并且等待结果的返回。
startActivityForResult(intent, 2);
}
//用于获取下一个界面返回的数据,这个方法由系统调用。
/*
* 参数一: 请求码
* 参数二: 结果码
* 参数三: 下一个界面传递过来的意图对象
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
String content = data.getStringExtra("content");
System.out.println("下一个界面返回数据了"+content);
et_content.setText(content);
}else if(requestCode == 2 ){
et_contact.settext(content);
}
}
}
第二个是短信模板界面所对应的activity代码:
package com.itheima.smshelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SmsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
final String[]objects = {
"这是短信内容----1",
"这是短信内容----2",
"这是短信内容----3",
"这是短信内容----4",
"这是短信内容----5",
"这是短信内容----6",
"这是短信内容----7",
"这是短信内容----8",
"这是短信内容----9",
"这是短信内容----10",
"这是短信内容----11"
};
ListView lv =(ListView)findViewById(R.id.lv);
//参数一:上下文 ,参数二: 使用的条目布局,这里使用的是系统内置的布局 ,参数三: 数据源,也就是显示到条目上的数据
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, objects));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(SmsActivity.this, "position==="+position, 0).show();
//获取到点击的条目内容
String content = objects[position];
//2. 设置返回的数据
Intent data = new Intent();
data.putExtra("content", content);
if(短信是春节短信){
setResult(10, data);
}else if(短信是中秋接短信){
setResult(11, data);
}
//3. 关闭当前界面
finish();
/*Intent intent = new Intent(SmsActivity.this, MainActivity.class);
intent.putExtra("content", content);
startActivity(intent);*/
}
});
}
}
第三个是号码选择对应的activity代码:
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ContactActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
final String[]objects = {
"13810081001",
"13810081002",
"13810081003",
"13810081004",
"13810081005",
"13810081006",
"13810081007",
"13810081008",
"13810081009",
"13810081010"
};
ListView lv =(ListView)findViewById(R.id.lv);
//参数一:上下文 ,参数二: 使用的条目布局,这里使用的是系统内置的布局 ,参数三: 数据源,也就是显示到条目上的数据
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, objects));
}
}
清单文件注册页面的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.smshelper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.smshelper.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.itheima.smshelper.SmsActivity"></activity>
<activity android:name="com.itheima.smshelper.ContactActivity"></activity>
</application>
</manifest>