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);
} }

android 16 带返回值的activity的更多相关文章

  1. Android课程---Activity 带返回值的跳转

    Activity2.java package com.hanqi.test4; import android.content.Intent; import android.os.Bundle; imp ...

  2. 064 01 Android 零基础入门 01 Java基础语法 08 Java方法 02 无参带返回值方法

    064 01 Android 零基础入门 01 Java基础语法 08 Java方法 02 无参带返回值方法 本文知识点:无参带返回值方法 说明:因为时间紧张,本人写博客过程中只是对知识点的关键步骤进 ...

  3. 自定义Dialog以及Dialog返回值到Activity

    步骤: 1.定义自定义的Dialog的布局文件 2.写一个类MyDialog继承Dialog 3.Dialog 返回值到Activity的方法是定义一个接口,接口中定义返回值到Activity的方法, ...

  4. 慕课网-Java入门第一季-7-3 Java 中无参带返回值方法的使用

    来源:http://www.imooc.com/code/1579 如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值 ...

  5. Java 中带参带返回值方法的使用

    如果方法既包含参数,又带有返回值,我们称为带参带返回值的方法. 例如:下面的代码,定义了一个 show 方法,带有一个参数 name ,方法执行后返回一个 String 类型的结果 调用带参带返回值的 ...

  6. Java 中无参带返回值方法的使用

    如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值为 int 类型的方法,执行的操作为计算两数之和,并返回结果 在 c ...

  7. EF5中 执行 sql语句使用Database.ExecuteSqlCommand 返回影响的行数 ; EF5执行sql查询语句 Database.SqlQuery 带返回值

    一: 执行sql语句,返回受影响的行数 在mysql里面,如果没有影响,那么返回行数为  -1 ,sqlserver 里面  还没有测试过 using (var ctx = new MyDbConte ...

  8. 测试 多线程 实现 callable 带返回值

    package threadTest; import java.util.ArrayList; import java.util.Date; import java.util.concurrent.C ...

  9. Mysql带返回值与不带返回值的2种存储过程

    过程1:带返回值: 1 drop procedure if exists proc_addNum; 2 create procedure proc_addNum (in x int,in y int, ...

随机推荐

  1. Spring 初学 1

    Spring是一个轻量级的框架,他有自己的MVC框架SpringMVC,在以往的Web项目中大多采用Structs2+hibernate+Spring的框架,Structs做web层,Hibernat ...

  2. io开发之C语言第二天

    开发环境是OS X系统下的Xcode Xcode的两个快捷键以及打开Xcode项目的正确方式 快捷键:command + B 编译 + 链接 快捷键:command + R 编译 + 链接 + 运行 ...

  3. iOS 图片转NSData-b

    iOS开发中 UIImage可能经常需要转为NSData 上传 传递等等 有两个比较常用的方法 UIImageJPEGRepresentation UIImagePNGRepresentation 第 ...

  4. ZeroBraneStudio之支持远程调试

    打开ZBS后,如果需要远程调试得先开启调试服务器:Project->Start Debugger Server 打开之后就可以编辑文件进行测试了.示例代码如下: local ZBS = 'D:/ ...

  5. constant属性详解

    /**是否使用开发模式,不在开发模式下变为false*/ (常用) <constant name = "struts.devmode" value = "true& ...

  6. Html和JS基础

    1.body:bgcolor,background(背景图片),bgproperities=fixed(图片水印),text(正文颜色). 2.hr:水平分割线,正文标题<h?>自动换行了 ...

  7. iOS7新特性-NSURLSession详解

    前言:本文由DevDiv版主@jas 原创翻译,转载请注明出处!原文:http://www.shinobicontrols.com/b ... day-1-nsurlsession/ 大家都知道,过去 ...

  8. German Collegiate Programming Contest 2013:E

    数值计算: 这种积分的计算方法很好,学习一下! 代码: #include <iostream> #include <cmath> using namespace std; ; ...

  9. MYSQL简单安装配置

    有用的URL: http://www.cnblogs.com/zeroone/articles/2298942.html http://blog.csdn.net/h1017597898/articl ...

  10. 实验一个最小的PYTHON服务器编程

    没事,玩玩儿~~~:) 按书上的例子来作.. #!/usr/bin/env python #Simple Server - Chapter 1 -server.py import socket hos ...