`

android之有返回结果跳转intent

 
阅读更多

android之有返回结果跳转intent:

(1、注意跳转的时候要传像user的对象必须实现Serializable接口,2、login的java代码中setResult(RESULT_OK, intent);后一定要调用finish()方法

主页面布局:layout/activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sxt.main.MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btLogin"
android:text="登陆"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btLogin"
android:id="@+id/btRegister"
android:text="注册"/>

</RelativeLayout>
主页面布局java代码:

package com.sxt.main;


import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
private static final int LOGINCODE = 0;
private static final int REGISTER = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setlistener();
}


private void setlistener() {
// TODO Auto-generated method stub
findViewById(R.id.btLogin).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivityForResult(intent, LOGINCODE);
}
});
}


//这是跳转到另一个布局页面返回来的操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!=RESULT_OK){
return;
}
switch (requestCode) {
case LOGINCODE:
User user = (User) data.getSerializableExtra("user");
Log.i("main", "注册信息是:"+user);
Toast.makeText(this,"注册信息是:"+user, 50000).show();
break;
case REGISTER:

break;
}
}






}


跳转到的登陆注册页面布局:

登陆页面布局:layout/activity_login.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编号"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etUser"
android:hint="请输入1-10个字符"
/>

</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etPassword"
android:hint="请输入1-10个字符"
android:password="true"/>

</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/register"
android:text="登陆"
android:drawableLeft="@drawable/login32x32"
android:background="@drawable/btn_bg"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/exit"
android:drawableLeft="@drawable/exit32x32"
android:background="@drawable/btn_bg"
android:text="退出"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
</LinearLayout>
</LinearLayout>

跳转到的登陆注册的java代码:

package com.sxt.main;


import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;


public class LoginActivity extends ActionBarActivity {
private EditText etUser,etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
init();
setListener();
}


private void setListener() {
// TODO Auto-generated method stub
findViewById(R.id.register).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
String code = etUser.getText().toString();
if(TextUtils.isEmpty(code)){
etUser.setError("编号不能为空");
return;
}
String passw = etPassword.getText().toString();
if(TextUtils.isEmpty(passw)){
etPassword.setError("密码不能为空");
return;
}
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
intent.putExtra("user", new User(code,passw));
setResult(RESULT_OK, intent);
finish();
}
});
findViewById(R.id.exit).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
finish();
}
});
}


private void init() {
// TODO Auto-generated method stub
etUser = (EditText) findViewById(R.id.etUser);
etPassword = (EditText) findViewById(R.id.etPassword);

}
}

实体类User:

package com.sxt.main;


import java.io.Serializable;


public class User implements Serializable{
private String code;
private String password;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String code, String password) {
super();
this.code = code;
this.password = password;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.code+","+this.password;
}

}

效果:

第一步点击登陆:

第二步填写信息:

第3步登陆成功返回到原来的布局页面:

 

分享到:
评论

相关推荐

    android ,跳转到代码(intent)

    本文将深入探讨Intent的概念、类型、创建与使用,以及如何通过Intent在Android应用程序中实现页面跳转。 Intent是Android系统中一个核心的概念,主要用于组件间的通信。在Android四大组件(Activity、Service、...

    Android通过Intent跳转地图应用(百度地图、高德地图)

    本篇文章将深入讲解如何使用Intent在Android应用中跳转到这些地图应用,并在用户未安装相应地图应用时提供备选方案,如打开网页版地图。 首先,我们需要了解Intent的基本结构。Intent通常由两部分组成:Action和...

    Android开发 两个Activity之间通过Intent跳转传值

    创建Intent主要有两种方式:显式Intent和隐式Intent。显式Intent通过指定目标Activity的完整类名来启动,适合在同一应用内部通信;隐式Intent则不指定具体的目标Activity,而是通过定义Action、Data、Category等元...

    Android使用Intent实现页面跳转

    什么是Intent Intent可以理解为信使(意图) 由Intent来协助完成Android各个组件之间的通讯 Intent实现页面之间的跳转 ...第二种启动方式可以返回结果 代码 FirstActivity.java public class FirstActiv

    Android Intent带返回值跳转Demo

    本教程将详细讲解如何通过Intent实现带返回值的Activity跳转,这对于Android开发者来说是非常基础且重要的技能。 一、Intent的基本概念 Intent是一种运行时的请求,用于表示应用中的一个动作,它可以携带数据。...

    Android Intent跳转和回传值

    Intent跳转传值,和Activity返回传值

    Android中界面间的跳转(两种方式)

    总结来说,`startActivity(Intent)`适合简单的Activity跳转,无需返回结果;而`startActivityForResult(Intent, int)`则适用于需要从新Activity获取结果的场景。了解并熟练掌握这两种方法,对进行Android应用的界面...

    Android从一个应用跳转到另一个应用

    或者,如果需要在返回结果后继续执行,可以使用`startActivityForResult()`。 5. **处理权限** 跳转到其他应用可能需要请求相应的权限。例如,如果要打开浏览器,需要在AndroidManifest.xml中声明`...

    android两个app实现跳转.rar

    可以使用模拟器或真实设备进行跳转测试,确保在各种情况下都能正确跳转并处理返回结果。 10. **代码示例**: 创建Intent的基本步骤如下: ```java Intent intent = new Intent(); intent.setComponent(new ...

    android之activity跳转 窗口跳转

    总结来说,Android中的Activity跳转是通过Intent进行的,可以显式或隐式启动,传递数据,选择不同的启动模式,同时还需要理解Activity的生命周期和返回栈管理,以便构建高效、用户体验良好的应用。在实际开发中,应...

    android系统广播大全 Intent跳转界面

    Android 系统广播大全 Intent 跳转界面 Android 系统广播大全 Intent 跳转界面是一种机制,允许应用程序之间进行交互和通信。Intent 是 Android 中的一种机制,可以将动作和数据封装起来,用于启动 Activity、服务...

    android-页面跳转-Activity&Intent详解,Bundle类介绍说明.doc

    Android 页面跳转和 Intent 详解,Bundle 类介绍说明 Android 页面跳转是移动应用程序中的一种基本交互方式,通过 Intent 和 Activity 两个组件来实现。Intent 是 Android 中的一个核心组件,用于在不同的 Activity...

    android应用跳转界面

    此外,Android提供了多种跳转方式,如使用`startActivityForResult()`来进行有结果返回的跳转,或者使用`Intent.FLAG_ACTIVITY_CLEAR_TOP`清除栈顶的Activity等。了解这些特性可以帮助开发者更好地控制应用的导航...

    Android2个App之间跳转

    以上就是关于Android两个App之间跳转的知识点,涉及Intent、Intent Filter、数据传递和权限管理等方面。通过Android Studio,开发者可以轻松实现不同应用间的交互,提升用户体验。在实际开发中,还需要考虑安全性和...

    Android 单击实现页面跳转

    在实际开发中,你可能需要考虑更多的细节,例如处理返回结果、设置动画效果、处理Activity之间的通信等。在URLTest这个示例项目中,你可以进一步探索这些概念,加深对Android页面跳转机制的理解。

    android两种方式页面跳转的下例子

    接下来,我们讨论携带结果的跳转,即有结果返回的跳转。这种方式通常用于一个Activity需要启动另一个Activity获取某些数据,然后将这些数据传回原始Activity。我们可以使用`startActivityForResult()`方法来实现。...

    Androidstudio实现页面跳转和传递参数

    首先,页面跳转在Android中主要通过Intent对象来实现。Intent是一种用来表达应用程序之间意图的类,可以理解为一个消息对象,它告诉系统你要执行什么操作。在实现页面跳转时,我们通常创建一个新的Intent实例,并...

    intent 页面跳转

    Intent页面跳转是Android应用中常见的功能,通常涉及Activity之间的交互。在这个场景下,我们点击一个按钮,从第一个页面(Activity)跳转到第二个页面(Activity)。下面将详细解释Intent的工作原理以及如何实现...

    Android Studio多个按钮跳转多个页面,利用选项卡功能实现

    本教程将详细介绍如何在Android Studio中实现一个功能,即通过多个按钮跳转到多个不同的页面,并利用选项卡功能来增强用户体验。首先,我们需要理解Android Studio中的布局设计、按钮事件监听以及选项卡控件的使用。...

    intent页面跳转

    5. **处理返回结果**:如果需要在新启动的Activity执行完后返回结果,可以使用`startActivityForResult(Intent, requestCode)`,并在新Activity中调用`setResult()`设置结果,最后在原Activity覆盖的`...

Global site tag (gtag.js) - Google Analytics