在Android中打开窗口有两种方式,第一种是不需要返回值的,第二种是带返回值的。

 

Main.xml文件,程序从这个窗口开始执行。

<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/btn_open1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一种方式打开" />

    <Button
        android:id="@+id/btn_open2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二种方式打开" />

</LinearLayout>

 

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一个窗口" />
    
</LinearLayout>

 

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二个窗口" />

    <Button
        android:id="@+id/btnresult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="回传数据"/>

</LinearLayout>

 

main.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button btn_open1;
    Button btn_open2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //第一种方式打开窗口,无返回值
        btn_open1 = (Button) findViewById(R.id.btn_open1);
        btn_open1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 第一个为当前类的上下文参数,不能直接使用this,需要使用类名.this
                // 第二个参数为目标文件的反射对象
                Intent intent = new Intent(MainActivity.this, FirstActivity.class);
                // 启动新窗口,不需要返回值
                startActivity(intent);
            }
        });

        //第二种打开方式,带返回值得
        btn_open2 = (Button) findViewById(R.id.btn_open2);
        btn_open2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 第一个为当前类的上下文参数,不能直接使用this,需要使用类名.this
                // 第二个参数为目标文件的反射对象
                Intent intent = new Intent(MainActivity.this, SecendActivity.class);

                // 第一个参数为intent
                // 第二个参数为请求的标志,用来区别提交的activity
                startActivityForResult(intent, 1);
            }
        });
    }

    /**
     * 通过这个方法用来接收新页面的返回数据,返回内容为Intent对象
     * 第一个参数为请求的id,第一个页面传递过来的标志,用来区别是哪个activity传递过来的。 第二个参数为结果的id,第二个页面返回的标志
     * 第三个参数为返回的intent对象,通过他获取跳转页面的返回内容
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        //requestCode判断是哪个按钮提交的请求,用来区分提交的请求
        //resultCode判断是哪个页面返回的结果,用来区分返回的页面请求。
        if (requestCode == 1 && resultCode == 2) {
            String content = data.getStringExtra("data");
            Toast.makeText(this, content, 1).show();  //弹出消息框
        }
    }

}

 

SecendActivity.java

package com.example.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecendActivity extends Activity {

    Button btn;
    String content = "第二个窗口返回的数据";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secend_activity);

        btn = (Button) findViewById(R.id.btnresult);

        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data = new Intent();  //创建返回的Intent对象
                data.putExtra("data", content);   //为intent对象设置值
                setResult(2, data);               //设置回传结果
                
                finish();     //关闭窗口
            }
        });

    }
}

 

 

点击第一个按钮会打开第一个窗口。

低级第二个按钮会打开第二个窗口,在第二个窗口中点击回传按钮,返回数据到第一个窗口,并关闭当前窗口。

posted on 2015-03-24 17:54  Builder  阅读(453)  评论(0编辑  收藏  举报