您好,登錄后才能下訂單哦!
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步登陸成功返回到原來的布局頁面:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。