数据返回值

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent">
  5 
  6     <TextView
  7     android:id="@+id/textView1"
  8     android:layout_width="wrap_content"
  9     android:layout_height="wrap_content"
 10     android:text=""/>
 11 
 12     <Button
 13         android:id="@+id/btn1"
 14         android:layout_width="180dp"
 15         android:layout_height="74dp"
 16         android:layout_centerInParent="true"
 17         android:onClick="regist"
 18         android:text="获取信息"
 19         android:textSize="25sp"/>
 20 
 21 </RelativeLayout>
 22 package com.example.datarerurn;
 23 
 24 import android.app.Activity;
 25 import android.content.Intent;
 26 import android.os.Bundle;
 27 import android.view.View;
 28 import android.widget.TextView;
 29 
 30 public class MainActivity extends Activity {
 31 
 32     @Override
 33     protected void onCreate(Bundle savedInstanceState) {
 34         super.onCreate(savedInstanceState);
 35         setContentView(R.layout.activity_main);
 36     }
 37     public void regist(View v) {
 38         // TODO Auto-generated method stub
 39         Intent intent=new Intent(this,RegistActivity.class);
 40         startActivityForResult(intent, 1);//当有多个请求的时候,标识的
 41 
 42     }
 43 
 44     @Override
 45     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
 46         // TODO Auto-generated method stub
 47         if(requestCode==1&&resultCode==1){
 48             String name=intent.getStringExtra("name");
 49             int age=intent.getIntExtra("age", 0);
 50             String text="你的姓名是"+name+"你的年龄是"+age;
 51 
 52             ((TextView)(findViewById(R.id.textView1))).setText(text);
 53         }
 54     }
 55 
 56 
 57 
 58 }
 59 ---------------------------------------------------------
 60 <?xml version="1.0" encoding="utf-8"?>
 61 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 62     xmlns:app="http://schemas.android.com/apk/res-auto"
 63     xmlns:tools="http://schemas.android.com/tools"
 64     android:layout_width="match_parent"
 65     android:layout_height="match_parent">
 66 
 67     <EditText
 68         android:id="@+id/editText1"
 69         android:layout_width="wrap_content"
 70         android:layout_height="wrap_content"
 71         android:layout_alignBaseline="@+id/textView1"
 72         android:layout_alignBottom="@+id/textView1"
 73         android:layout_alignParentRight="true"
 74         android:layout_marginRight="19dp"
 75         android:layout_marginBottom="-4dp"
 76         android:ems="10" />
 77 
 78     <TextView
 79         android:id="@+id/textView1"
 80         android:layout_width="wrap_content"
 81         android:layout_height="wrap_content"
 82         android:layout_alignParentLeft="true"
 83         android:layout_alignParentTop="true"
 84         android:layout_marginLeft="60dp"
 85         android:layout_marginTop="81dp"
 86         android:text="输入姓名"
 87         android:textSize="25sp" />
 88 
 89     <EditText
 90         android:id="@+id/editText2"
 91         android:layout_width="wrap_content"
 92         android:layout_height="wrap_content"
 93         android:layout_alignLeft="@+id/editText1"
 94         android:layout_below="@+id/editText1"
 95         android:layout_marginTop="33dp"
 96         android:ems="10" />
 97 
 98     <TextView
 99         android:id="@+id/textView2"
100         android:layout_width="wrap_content"
101         android:layout_height="wrap_content"
102         android:layout_alignBaseline="@+id/editText2"
103         android:layout_alignBottom="@+id/editText2"
104         android:layout_alignLeft="@+id/textView1"
105         android:text="输入年龄"
106         android:textSize="25sp"/>
107 
108     <Button
109         android:id="@+id/button1"
110         android:layout_width="136dp"
111         android:layout_height="74dp"
112         android:layout_below="@+id/editText2"
113         android:layout_centerHorizontal="true"
114         android:layout_marginTop="91dp"
115         android:onClick="click"
116         android:text="返回"
117         android:textSize="25sp" />
118 
119 </RelativeLayout>
120 package com.example.datarerurn;
121 
122 
123 import android.app.Activity;
124 import android.content.Intent;
125 import android.os.Bundle;
126 import android.view.View;
127 import android.widget.EditText;
128 
129 public class RegistActivity extends Activity {
130     protected void onCreate(Bundle savedInstanceState) {
131         super.onCreate(savedInstanceState);
132         setContentView(R.layout.regist);
133     }
134 
135     public void click(View v) {
136             Intent intent = new Intent();
137             String name = ((EditText) findViewById(R.id.editText1)).getText()
138                     .toString();
139             int age = Integer.parseInt(((EditText) findViewById(R.id.editText2))
140                     .getText().toString());
141             intent.putExtra("name", name);
142             intent.putExtra("age", age);
143         setResult(1, intent);
144         finish();
145 
146     }
147 
148 }
原文地址:https://www.cnblogs.com/WangYYY/p/14023694.html