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);
} }
android 16 带返回值的activity的更多相关文章
- Android课程---Activity 带返回值的跳转
Activity2.java package com.hanqi.test4; import android.content.Intent; import android.os.Bundle; imp ...
- 064 01 Android 零基础入门 01 Java基础语法 08 Java方法 02 无参带返回值方法
064 01 Android 零基础入门 01 Java基础语法 08 Java方法 02 无参带返回值方法 本文知识点:无参带返回值方法 说明:因为时间紧张,本人写博客过程中只是对知识点的关键步骤进 ...
- 自定义Dialog以及Dialog返回值到Activity
步骤: 1.定义自定义的Dialog的布局文件 2.写一个类MyDialog继承Dialog 3.Dialog 返回值到Activity的方法是定义一个接口,接口中定义返回值到Activity的方法, ...
- 慕课网-Java入门第一季-7-3 Java 中无参带返回值方法的使用
来源:http://www.imooc.com/code/1579 如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值 ...
- Java 中带参带返回值方法的使用
如果方法既包含参数,又带有返回值,我们称为带参带返回值的方法. 例如:下面的代码,定义了一个 show 方法,带有一个参数 name ,方法执行后返回一个 String 类型的结果 调用带参带返回值的 ...
- Java 中无参带返回值方法的使用
如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值为 int 类型的方法,执行的操作为计算两数之和,并返回结果 在 c ...
- EF5中 执行 sql语句使用Database.ExecuteSqlCommand 返回影响的行数 ; EF5执行sql查询语句 Database.SqlQuery 带返回值
一: 执行sql语句,返回受影响的行数 在mysql里面,如果没有影响,那么返回行数为 -1 ,sqlserver 里面 还没有测试过 using (var ctx = new MyDbConte ...
- 测试 多线程 实现 callable 带返回值
package threadTest; import java.util.ArrayList; import java.util.Date; import java.util.concurrent.C ...
- Mysql带返回值与不带返回值的2种存储过程
过程1:带返回值: 1 drop procedure if exists proc_addNum; 2 create procedure proc_addNum (in x int,in y int, ...
随机推荐
- 图片延迟加载插件jquery.lazyload.js的使用方法
最新版的jquery.lazyload.js已不再是伪的延迟加载了 一.请按照基本使用方法说明设置 //载入JavaScript 文件 <script src="jquery.js&q ...
- smarty模板引擎原理解析
//php 控制器文件 <?php//引入模板引擎文件include("20130304.php");$smarty =newTinySmarty();$qq_numbers ...
- 用三或四个个div标签实现工字效果
使用重构的方式制作出一个如下图的水平.垂直都居中,短边为50px,长边为150px的红色“工”字. a) 使用3个div完成 <!DOCTYPE html><html lang=&q ...
- 从UI Automation看Windows平台自动化测试原理
前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...
- HTML+JS版本的俄罗斯方块
<!doctype html><html><head></head><body> <div id="box" st ...
- Early 80386 CPUs
Assembling a detailed and accurate history of the 80386, including a complete listing of all the &qu ...
- [jobdu]调整数组顺序使奇数位于偶数前面
这道题的代码没啥好说的,用了O(n)的空间就是水题了.但可以讲一下思考过程.一开始是想O(1)的空间的,然后想从左往右双指针扫,然后根据出现顺序交换遇到的偶数和奇数.但遇到一个问题:1, 2, 3, ...
- Apache OFBiz
Apache OFBiz® Apache OFBiz offers a great deal of functionality, including: advanced e-commerce cata ...
- 2.5.4 使用popupWindow
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- BZOJ1232: [Usaco2008Nov]安慰奶牛cheer
1232: [Usaco2008Nov]安慰奶牛cheer Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 578 Solved: 403[Submi ...