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

}

效果:

第一步点击登陆:

android之有返回结果跳转intent

第二步填写信息:

android之有返回结果跳转intent

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

android之有返回结果跳转intent

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网页题目:android之有返回结果跳转intent-创新互联
网页地址:https://www.cdcxhl.com/article24/jsgje.html

成都网站建设公司_创新互联,为您提供外贸网站建设微信小程序服务器托管云服务器网站导航搜索引擎优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站网页设计