android 16 带返回值的activity

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/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆" />
    <Button
        android:id="@+id/btnRegister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册" />
    
</LinearLayout>

login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1" >

        <TableRow>

            <TextView android:text="输入编号" />

            <EditText android:hint="2-10个字符" 
                android:id="@+id/etId"/>
        </TableRow>

        <TableRow>

            <TextView android:text="密码" />

            <EditText
                android:id="@+id/etPwd"
                android:hint="2-10个字符"
                android:password="true" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0,1" >

        <TableRow>

            <Button
                android:id="@+id/btnLogin"
                android:background="@drawable/btn_bg"
                android:drawableLeft="@drawable/login32x32"
                android:padding="3dp"
                android:text="登陆" 
                android:textColor="#fff"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:id="@+id/btnExit"
                android:background="@drawable/btn_bg"
                android:drawableLeft="@drawable/exit32x32"
                android:padding="3dp"
                android:text="退出" 
                android:textColor="#fff"
                android:layout_gravity="center_horizontal"/>
        </TableRow>
    </TableLayout>

</LinearLayout>

 mainActivity

package com.sxt.day04_02;

import com.sxt.day04_02.entity.User;

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

public class MainActivity extends Activity {
    static final int ACTION_LOGIN=0;
    static final int ACTION_REGISTER=10;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setListener();//设置监听器
    }

    //设置监听器
    private void setListener() {
        setLoginClickListener();
        setRegisterClickListener();
    }

    private void setRegisterClickListener() {
        findViewById(R.id.btnRegister).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });
    }

    private void setLoginClickListener() {
        findViewById(R.id.btnLogin).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, LoginActivity.class);//当前activity和目标activity
                startActivityForResult(intent, ACTION_LOGIN);//启动LoginActivity并且要求他返回结果,ACTION_LOGIN请求码0,
            }
        });
    }
    
    @Override//接收acticity返回的结果
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {//requestCode是请求码,就是这里的ACTION_LOGIN,resultCode是loginactivity的返回值OK,data是loginactivity的Intent对象。
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!=RESULT_OK){
            return ;
        }
        switch (requestCode) {
        case ACTION_LOGIN:
            User user=(User) data.getSerializableExtra("user");
            Log.i("main",user.toString());
            break;
        case ACTION_REGISTER:
            
            break;
        }
    }
}

loginActivity:

package com.sxt.day04_02;

import com.sxt.day04_02.entity.User;

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

public class LoginActivity extends Activity {
    EditText metId,metPwd;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        setListener();
    }

    private void setListener() {
        setLoginClickListener();
        setExitClickListener();
    }

    private void setExitClickListener() {//退出则不返回结果,
        findViewById(R.id.btnExit).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setLoginClickListener() {
        findViewById(R.id.btnLogin).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String id=metId.getText().toString();
                if(TextUtils.isEmpty(id)){
                    metId.setError("编号不能为空");
                    return ;
                }
                String pwd=metPwd.getText().toString();
                if(TextUtils.isEmpty(pwd)){
                    metPwd.setError("密码不能为空");
                    return ;
                }
                User user=new User(Integer.parseInt(id), pwd);
                Intent data=new Intent(LoginActivity.this, MainActivity.class);//当前activity和返回的activity
                data.putExtra("user", user);
                setResult(RESULT_OK, data);//设置返回结果,
                finish();//关闭当前activity
            }
        });
    }

    private void initView() {
        metId=(EditText) findViewById(R.id.etId);
        metPwd=(EditText) findViewById(R.id.etPwd);
    }

}

 

posted @ 2015-10-16 01:03  无天666  阅读(410)  评论(0编辑  收藏  举报