页面跳转(带返回参数的)---------android

在网上看了很多的按钮点击事件,,,都是配置监听什么的。。。。。我用的不是配置监听。

是和winform事件相似的方法,首先要有两个界面,在界面的button中添加onclick事件:

这是第一个主界面中的按钮
<
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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="有返回值的按钮" android:id="@+id/button" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:onClick="button1_click"/> </RelativeLayout>
这是第一个主界面中的方法
public void button1_click(View v) { //这个相当于一个信使,this是当前页面, //第二个参数是要跳转的页面 //这里的intent需要跳转页面,所以需要参数。。。在不需要跳转页面的时候,,不需要参数的。这个类也可以用作其他用途 Intent inten = new Intent(this, Main2Activity.class); //第二个参数只是请求的一个标志,如果不需要返回值,也就不需要这个方法了 //有可能这个页面会有很多个其他的请求,所有请求都会通过下面那个onActivityResult方法来获得那个页面返回的值 //所以这个表示,就在获取返回值的时候,用于判断,以对应正确的请求 startActivityForResult(inten, 1); } //这里的requestCode参数,就是上面设置的 1 ,当跳转的页面返回的时候,通过这个加以判断 //resultCode ,这个参数是在跳转的页面里面规定的,它也是一个int类型的标志 //第三个参数包含了返回的值 //如果不需要所跳转的页面返回值,也就不需要这个方法了 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == 2)//之前提到的两个标志,在这里显示出了作用 { new AlertDialog.Builder(this) .setTitle(data.getStringExtra("data"))//在跳转页面返回的键值对的键 .show(); } }

一下是跳转页面的界面和后台代码

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.myapplication.Main2Activity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回一个值,给上一个页面"
        android:id="@+id/button3"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:onClick="button1_click"/>
</RelativeLayout>
 public void button1_click(View v)
    {
        Intent inte = new Intent();
        inte.putExtra("data","你好");//这里是键值对,“data”是键
        setResult(2,inte);//返回值,2 是改返回的标志,也会返回
        finish();//销毁当前页面
    }

到此为止,安卓的又返回和无返回的页面跳转方式就结束了。

原文地址:https://www.cnblogs.com/xiaoleye/p/4768255.html