package com.lxj.lesson2_3ID19;

import com.example.lesson2_3_id19.R;
import com.lxj.other.AgeActivity;
import com.lxj.other.HeightActivity;
import com.lxj.other.SexActivity;

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

public class MainActivity extends Activity implements OnClickListener {
    private static final int REQUEST_AGE = 1;
    private static final int REQUEST_HEIGHT = 2;
    private static final int REQUEST_SEX = 3;
    User user = new User();
    TextView tvAge,tvHeight,tvSex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initView();
        registerListener();
    }
    private void registerListener() {
        tvAge.setOnClickListener(this);
        tvHeight.setOnClickListener(this);
        tvSex.setOnClickListener(this);
    }
    private void initView() {
        tvAge = (TextView) findViewById(R.id.tv_age);
        tvHeight = (TextView) findViewById(R.id.tv_height);
        tvSex = (TextView) findViewById(R.id.tv_sex);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.tv_age:
            startActivityForResult(new Intent(this, AgeActivity.class), REQUEST_AGE);
            break;
        case R.id.tv_height:
            startActivityForResult(new Intent(this, HeightActivity.class), REQUEST_HEIGHT);
            break;
        case R.id.tv_sex:
            startActivityForResult(new Intent(this, SexActivity.class), REQUEST_SEX);
            break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 调试可见,程序中不用
        Log.e("TAG", "-------------程序从" + requestCode + "返回");
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case REQUEST_AGE:
                String age = data.getStringExtra("age");
                tvAge.setText(age);
                break;
            case REQUEST_HEIGHT:
                String height = data.getStringExtra("height");
                tvHeight.setText(height);
                break;
            case REQUEST_SEX:
                String sex = data.getStringExtra("sex");
                tvSex.setText(sex);
                break;
            }
        }else {
            // 调试程序用log,代码中不需要
            Log.e("TAG", "-------------程序没有任何返回");
        }
    }
}
package com.lxj.lesson2_3ID19;

public class User {
    String age;
    String height;
    String sex;
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public User(String age, String height, String sex) {
        super();
        this.age = age;
        this.height = height;
        this.sex = sex;
    }
    
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "User [age=" + age + ", height=" + height + ", sex=" + sex + "]";
    }
    
}
package com.lxj.other;

import com.example.lesson2_3_id19.R;

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

public class AgeActivity extends Activity implements OnClickListener{
    TextView aga1,age2,age3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_age);
        initView();
        registerListener();
    }
    private void registerListener() {
        aga1.setOnClickListener(this);
        age2.setOnClickListener(this);
        age3.setOnClickListener(this);
    }
    private void initView() {
        aga1 = (TextView) findViewById(R.id.tv_age_1);
        age2 = (TextView) findViewById(R.id.tv_age_2);
        age3 = (TextView) findViewById(R.id.tv_age_3);
    }
    @Override
    public void onClick(View v) {
        // 这个v代表当前所点击的视图
        // instanceof:代表 左边的对象是否是右边类型的实例
        if (v instanceof TextView) {
            // 把v强转成TextView类型
            TextView tv = (TextView) v;
            
            //带参返回的步骤
            Intent intent = getIntent();
            // tv.getText()中的数据返回回去
            // 将返回值放进去
            intent.putExtra("age", tv.getText());
            // setResult();
            // 请求成功
            // 设置返回码和返回值
            setResult(RESULT_OK, intent);
            // setResult要配合finish使用
            finish();
        }
    }
    
}
package com.lxj.other;

import com.example.lesson2_3_id19.R;

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

public class HeightActivity extends Activity implements OnClickListener{
    TextView tv_height_1,tv_height_2,tv_height_3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 找不到布局
        // 1. 布局和代码不在一个工程
        // 2. android.R
        setContentView(R.layout.activity_height);
        tv_height_1 = (TextView) findViewById(R.id.height_1);
        tv_height_2 = (TextView) findViewById(R.id.height_2);
        tv_height_3 = (TextView) findViewById(R.id.height_3);
        
        // 用this就需要实现OnClickListener
        tv_height_1.setOnClickListener(this);
        tv_height_2.setOnClickListener(this);
        tv_height_3.setOnClickListener(this);
        
    }
    @Override
    public void onClick(View v) {
        // 强转v成TextView        
        TextView tv = (TextView) v;
        
        //带参返回的步骤
        Intent intent = getIntent();
        intent.putExtra("height", tv.getText());
        setResult(RESULT_OK, intent);
        finish();
    }
}
package com.lxj.other;

import com.example.lesson2_3_id19.R;

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

public class SexActivity extends Activity implements OnClickListener{
    TextView tv_sex_1,tv_sex_2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sex);
        tv_sex_1 = (TextView) findViewById(R.id.sex_1);
        tv_sex_2 = (TextView) findViewById(R.id.sex_2);
        
        // 实现OnClickListener
        tv_sex_1.setOnClickListener(this);
        tv_sex_2.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        TextView tv = (TextView) v;
        Intent intent = getIntent();
        intent.putExtra("sex", tv.getText());
        // 设置返回码返回值
        setResult(RESULT_OK, intent);
        finish();
    }
}
<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="张三"
        android:textSize="20dp"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/tv_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="年龄"
        android:textSize="18dp"
        android:layout_margin="10dp" />

    <TextView
        android:id="@+id/tv_height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="身高"
        android:textSize="18dp"
        android:layout_margin="10dp"  />

    <TextView
        android:id="@+id/tv_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="性别"
        android:textSize="18dp"
        android:layout_margin="10dp"  />

</LinearLayout>
<?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/tv_age_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="18岁以下"
        android:textSize="18dp" />

    <TextView
        android:id="@+id/tv_age_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="18—30岁"
        android:textSize="18dp" />

    <TextView
        android:id="@+id/tv_age_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="30岁以上"
        android:textSize="18dp" />

</LinearLayout>
<?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/height_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="170cm以下"
        android:textSize="18dp" 
        android:layout_margin="10dp" />

    <TextView
        android:id="@+id/height_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="170-180cm"
        android:textSize="18dp" 
        android:layout_margin="10dp" />

    <TextView
        android:id="@+id/height_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="180cm以上"
        android:textSize="18dp" 
        android:layout_margin="10dp" />

</LinearLayout>
<?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/sex_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="男"
        android:textSize="18dp" />

    <TextView
        android:id="@+id/sex_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="女"
        android:textSize="18dp" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lesson2_3_id19"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.lxj.lesson2_3ID19.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name="com.lxj.other.AgeActivity">
             </activity>
                     <activity 
            android:name="com.lxj.other.HeightActivity">
             </activity>
                     <activity 
            android:name="com.lxj.other.SexActivity">
             </activity>
    </application>

</manifest>