1.多个Activity界面实现数据的传递
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#565968">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="用户名:"/>
<EditText
android:id="@+id/et1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tv1"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="密码:"
android:layout_below="@id/tv1"/>
<EditText
android:id="@+id/et2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@id/tv1"
android:layout_toRightOf="@id/tv1"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:layout_below="@id/tv2"
android:layout_marginTop="29dp"/>
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/tv2"
android:layout_toRightOf="@id/tv3"
android:layout_marginTop="25dp">
<RadioButton
android:id="@+id/boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:layout_below="@id/tv3"
android:layout_marginTop="29dp"/>
<CheckBox
android:id="@+id/ping-pong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球"
android:textSize="12dp"
android:layout_below="@id/tv4"
/>
<CheckBox
android:id="@+id/football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球"
android:textSize="12dp"
android:layout_marginLeft="75dp"
android:layout_below="@id/tv4"
/>
<CheckBox
android:id="@+id/volleyball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排球"
android:textSize="12dp"
android:layout_marginLeft="135dp"
android:layout_below="@id/tv4"
/>
<CheckBox
android:id="@+id/badminton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="羽毛球"
android:textSize="12dp"
android:layout_marginLeft="195dp"
android:layout_below="@id/tv4"
/>
<CheckBox
android:id="@+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球"
android:textSize="12dp"
android:layout_marginLeft="265dp"
android:layout_below="@id/tv4"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textColor="#896526"
android:layout_below="@id/tv4"
android:layout_marginTop="100dp"
android:layout_marginLeft="120dp"
android:onClick="click"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myhomework4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
Intent it1=new Intent(this,SecondActivity.class);
String username = ((EditText)findViewById(R.id.et1)).getText().toString();
String password = ((EditText)findViewById(R.id.et2)).getText().toString();
it1.putExtra("username", username);
it1.putExtra("password", password);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg1);
int id = radioGroup.getCheckedRadioButtonId();
if(id == R.id.boy)
{
it1.putExtra("sex","男");
}
else
{
it1.putExtra("sex","女");
}
CheckBox checkBox1 = (CheckBox)findViewById(R.id.ping_pong);
CheckBox checkBox2 = (CheckBox)findViewById(R.id.football);
CheckBox checkBox3 = (CheckBox)findViewById(R.id.volleyball);
CheckBox checkBox4 = (CheckBox)findViewById(R.id.badminton);
CheckBox checkBox5 = (CheckBox)findViewById(R.id.basketball);
if(checkBox1.isChecked()) {
it1.putExtra("ping-pong","乒乓球");
}
if (checkBox2.isChecked()) {
it1.putExtra("football","足球");
}
if(checkBox3.isChecked()) {
it1.putExtra("volleyball", "排球");
}
if(checkBox4.isChecked()) {
it1.putExtra("badminton", "羽毛球");
}
if (checkBox5.isChecked()){
it1.putExtra("basketball","篮球");
}
startActivity(it1);
}
}
package com.example.myhomework4;
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
public class SecondActivity extends Activity {
@Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");
String sex = intent.getStringExtra("sex");
String pingpong ="";
String football ="";
String volleyball ="";
String badminton ="";
String basketball ="";
if(intent.getStringExtra("ping-pong") != null)
{
pingpong = intent.getStringExtra("ping-pong");
}
if(intent.getStringExtra("football") != null)
{
football = intent.getStringExtra("football");
}
if(intent.getStringExtra("volleyball") != null)
{
volleyball = intent.getStringExtra("volleyball");
}
if(intent.getStringExtra("badminton") != null)
{
badminton = intent.getStringExtra("badminton");
}
if(intent.getStringExtra("basketball") != null)
{
basketball = intent.getStringExtra("basketball");
}
Toast.makeText(this, "您好,您的注册信息是:\n\n" + "用户名:" + username + "\n密码:" + password + "\n性别:"
+ sex + "\n爱好:" + pingpong + football + volleyball + badminton + basketball, Toast.LENGTH_SHORT).show();
}
}


浙公网安备 33010602011771号