android之有返回结果跳转intent
android之有返回结果跳转intent:
创新互联建站长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为呼和浩特企业提供专业的成都网站制作、成都网站建设,呼和浩特网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
(1、注意跳转的时候要传像user的对象必须实现Serializable接口,2、login的java代码中setResult(RESULT_OK, intent);后一定要调用finish()方法)
主页面布局:layout/activity_main.xml:
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" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btLogin"
android:text="登陆"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btLogin"
android:id="@+id/btRegister"
android:text="注册"/>
主页面布局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:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_height="wrap_content"
android:text="编号"/>
android:layout_height="wrap_content"
android:id="@+id/etUser"
android:hint="请输入1-10个字符"
/>
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_height="wrap_content"
android:text="密码"/>
android:layout_height="wrap_content"
android:id="@+id/etPassword"
android:hint="请输入1-10个字符"
android:password="true"/>
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
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"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
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="退出"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
跳转到的登陆注册的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
标题来源:http://azwzsj.com/article/pipopd.html