之前,我们讲到了学习Android需要的开发环境的搭建,今天我们来做一个小例子,来看一下Android是如何实现页面之间的参数传递的。
一、前端页面配置
1、activity_main页面
<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="com.example.intent.MainActivity" >
<!-- 设置控件宽度 -->
<!-- 设置控件高度 -->
<!-- 设置控件文本显示,读取values/strings.xml中的配置 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go" />
</RelativeLayout>
2、second页面
<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="com.example.intent.MainActivity" >
<TextView
android:id="@+id/secondtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second" />
<Button
android:id="@+id/secondbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
</RelativeLayout>
3、配置AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".SecondActivity"
android:label="@string/app_name">
</activity>
</application>
二、activity编写
1、MainActivity类
public class MainActivity extends Activity {
//声明按钮控件
private Button mainButton=null;
//本页面传递时的按钮btn的标志码
private final static int request_Code=11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置显示的页面
setContentView(R.layout.activity_main);
//根据配置的按钮id找到按钮控件
mainButton=(Button)findViewById(R.id.btn);
//为按钮设置单击的事件监听器
mainButton.setOnClickListener(listener);
}
//单击的事件监听器
private OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View arg0) {
//Intent是系统各组件之间进行数据传递的数据负载者,即通信使者
Intent intent=new Intent();
//设置页面转向
intent.setClass(MainActivity.this,SecondActivity.class);
//设置传递参数
intent.putExtra("str", "你好");
//startActivity(intent);不需要接收返回值时使用
//接收返回值,request_Code代表当前按钮btn的请求吗
startActivityForResult(intent, request_Code);
}
};
//重写接收返回值得方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//如果请求码等于按钮btn的标志码,则继续处理
if(requestCode==request_Code) {
//如果结果码,等于second页面中按钮btnButton的标志码,则继续处理
if(resultCode==SecondActivity.result_Code)
{
//从传回的数据中取得bundle
Bundle bundle=data.getExtras();
//得到返回值
String string=bundle.getString("back");
//显示返回值
Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2、SecondActivity 类
public class SecondActivity extends Activity {
//声明控件
private TextView textView;
private Button btnButton=null;
//此页面按钮btnButton的标志码
public final static int result_Code=1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//调用并显示页面2
setContentView(R.layout.second);
//得到intent通信使者
Intent intent=getIntent();
//得到bundle,存放数据的对象,类似于map
Bundle bundle=intent.getExtras();
//得到传过来的数据
String str=bundle.getString("str");
//得到textView控件
textView=(TextView)findViewById(R.id.secondtxt);
//显示传过来的数据
textView.setText(str);
//得到按钮,设置监听器
btnButton=(Button)findViewById(R.id.secondbtn);
btnButton.setOnClickListener(listener);
}
//单击事件监听器
private OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View arg0) {
//创建通信使者
Intent intent =new Intent();
//设置返回信息
intent.putExtra("back", "我崔晓光又回来了!!");
//设置返回码和通信使者
setResult(result_Code,intent);
//完成,关闭当前页面并返回
finish();
}
};
}
总结:
这里我们学会了在不同的页面之间传递参数。从中我们学到了intent和bundle对象,他们是参数传递的核心对象。在代码中我们多次使用到了标志码——requestCode和resqonseCode,我们要充分理解这两个码,其实就是对一个按钮的标志,根据这个我们可以处理同一个页面不同按钮的不同请求。通过这个小例子,希望大家对android开发有一定了解,为以后开发大型项目打好基础。