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步登陆成功返回到原来的布局页面: