数据返回值

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""/>

    <Button
        android:id="@+id/btn1"
        android:layout_width="180dp"
        android:layout_height="74dp"
        android:layout_centerInParent="true"
        android:onClick="regist"
        android:text="获取信息"
        android:textSize="25sp"/>

</RelativeLayout>
package com.example.datarerurn;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void regist(View v) {
        // TODO Auto-generated method stub
        Intent intent=new Intent(this,RegistActivity.class);
        startActivityForResult(intent, 1);//当有多个请求的时候,标识的

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // TODO Auto-generated method stub
        if(requestCode==1&&resultCode==1){
            String name=intent.getStringExtra("name");
            int age=intent.getIntExtra("age", 0);
            String text="你的姓名是"+name+"你的年龄是"+age;

            ((TextView)(findViewById(R.id.textView1))).setText(text);
        }
    }



}
---------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="19dp"
        android:layout_marginBottom="-4dp"
        android:ems="10" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="81dp"
        android:text="输入姓名"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="33dp"
        android:ems="10" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignLeft="@+id/textView1"
        android:text="输入年龄"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="136dp"
        android:layout_height="74dp"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="91dp"
        android:onClick="click"
        android:text="返回"
        android:textSize="25sp" />

</RelativeLayout>
package com.example.datarerurn;


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

public class RegistActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regist);
    }

    public void click(View v) {
            Intent intent = new Intent();
            String name = ((EditText) findViewById(R.id.editText1)).getText()
                    .toString();
            int age = Integer.parseInt(((EditText) findViewById(R.id.editText2))
                    .getText().toString());
            intent.putExtra("name", name);
            intent.putExtra("age", age);
        setResult(1, intent);
        finish();

    }

}

 

posted @ 2020-11-18 18:12  喜欢晒完太阳的被子  阅读(55)  评论(0编辑  收藏  举报