小Demo
以下是使用Intent传递数据的小Demo
A.Activity
public class A extends Activity {
EditText nameEdit;
EditText pwdEdit;
Button sentBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
nameEdit= (EditText )findViewById(R.id.user_edit);
pwdEdit = (EditText )findViewById(R.id.pwd_edit);
sentBtn = (Button)findViewById(R.id.sent_btn);
initActivity();
}
public void initActivity() {
sentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user_name, user_pwd;
user_name = nameEdit.getText().toString().trim();
user_pwd = pwdEdit.getText().toString().trim();
if (user_name.equals("user") && user_pwd.equals("123456")) {
Intent intent = new Intent();
intent.setClass(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString("user_name", user_name);
bundle.putString("user_pwd", user_pwd);
intent.putExtras(bundle);
startActivity(intent);
} else {
Toast.makeText(A.this, "登录失败!", Toast.LENGTH_LONG).show();
}
}
});
}
}
activity_a .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="25dp">
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="25sp"/>
<EditText
android:id="@+id/user_edit"
android:layout_toRightOf="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/user_pwd"
android:layout_below="@+id/user_name"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25sp"/>
<EditText
android:id="@+id/pwd_edit"
android:layout_below="@+id/user_edit"
android:layout_toRightOf="@+id/user_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<Button
android:id="@+id/sent_btn"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_below="@+id/user_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
</RelativeLayout>
B.Activity
public class B extends Activity {
TextView showText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
showText = (EditText )findViewById(R.id.show_text);
getActivity();
}
public void getActivity() {
Intent intent = this.getIntent();
String user_name = intent.getExtras().getString("user_name");
String user_pwd = intent.getExtras().getString("user_pwd");
showText.setText("用户:" + user_name + "登录成功!" + "且密码为:" + user_pwd);
}
}
activity_b .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/show_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="18sp" />
</RelativeLayout>