在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();     //关闭窗口            }        });    }}

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

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

更多相关文章

  1. Android JNI和NDK学习(09)--JNI实例二 传递类对象
  2. Android GWES 窗口系统
  3. Android 通过intent 传递对象
  4. Android 实现一个http 带参数登录
  5. 服务器向Android写一个对象
  6. Android窗口浮在屏幕上效果
  7. Android窗口机制(一)——Window,PhoneWindow,DecorView理解
  8. Android那些疑惑(2)-LayoutInflater中inflate方法参数的意义

随机推荐

  1. Android(安卓)小項目之---Toast對象詳細
  2. FFmpeg的Android平台移植—编译篇
  3. activity设置背景色为透明
  4. Android 通过WebView来播放flash在线视频
  5. Android Studio中设置ButterKnife、andro
  6. ViewPager的定时滚动,动态加载数据
  7. android Uri获取真实路径转换成File的方
  8. Android如何在java代码中设置margin
  9. android 加边框
  10. android panellistview 圆角实现代码