您的位置:首页 > 移动开发 > Android开发

android开发中是用Intent从一个activity跳向一个activity的问题

2016-03-06 14:44 441 查看
安卓中经常会使用页面跳转的问题,在安卓中主要有需要返回参数和不需要返回参数的两种方式,提醒大家在写了activity以后,要记得在AndroidManifest中声明。

代码注释如下:

第一个界面

public class MainActivity extends Activity {

// 定义按钮,文本框

private Button button;

private Button button1;

private TextView tv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

/*

* 通过fbutton实现页面之间的跳转

*/

button = (Button) findViewById(R.id.fbutton);

tv = (TextView) findViewById(R.id.textView1);

// 注册点击事件

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

// 初始化Intent,第一个参数,上下文对象this,第二个参数目标文件

Intent intent = new Intent(MainActivity.this, Factivity.class);

// startActivity的方式来实现

startActivity(intent);

}

});

button1 = (Button) findViewById(R.id.sbutton);

// 为button1设置监听事件

button1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

Intent intent = new Intent(MainActivity.this, Factivity.class);

// 第一个参数为intent和没有返回值得startActivity差不多,第二个参数是RequestCode,也就是请求码

startActivityForResult(intent, 1);

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

// 结果码为另一个activity里面setResult方法实现

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1 && resultCode == 2) {

String s = data.getStringExtra("data");

tv.setText(s);

}

}

}

第二个界面

public class Factivity extends Activity{

Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.factivity);

button=(Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent data =new Intent();

//putExtra和数据结构中的map一样,前面为键值,后面为对应的值

data.putExtra("data", "hello");

//setResult第一个参数为结果码,data本质为一个intent

setResult(2, data);

//关闭当前页面

finish();

}

});

}

}

第一个界面的xml布局文件

<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" >

<Button

android:id="@+id/fbutton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginTop="14dp"

android:text="第一种启动方式" />

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/fbutton"

android:layout_centerVertical="true"

android:text="记录activity返回的值" />

<Button

android:id="@+id/sbutton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/fbutton"

android:layout_below="@+id/fbutton"

android:layout_marginTop="34dp"

android:text="第二种启动方式" />

</RelativeLayout>

第二个界面的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" >

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="第一种启动方式" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: