<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="100dp"
android:paddingLeft="40dp">
<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="鐢ㄦ埛鍚嶏細"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_user"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="璇疯緭鍏ョ敤鎴峰悕"
android:textColor="#ccc"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="瀵? 鐮侊細"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_password"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="璇疯緭鍏ュ瘑鐮?
android:textColor="#ccc"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="鎬? 鍒細"
android:textSize="20sp" />
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/sex_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="鐢? />
<RadioButton
android:id="@+id/sex_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="濂?
android:layout_marginLeft="20dp"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/hobby"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="鐖? 濂斤細"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
<CheckBox
android:id="@+id/hb_badminton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缇芥瘺鐞?
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<CheckBox
android:id="@+id/hb_basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绡悆"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<CheckBox
android:id="@+id/hb_baseball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="鎺掔悆"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="鐧诲綍"
android:textColor="#fff"
android:textSize="25sp"
android:background="#2283f3"
android:layout_marginTop="50dp"
android:layout_marginLeft="60dp"
android:onClick="onClick"/>
</LinearLayout>
package com.example.show;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private EditText et_user, et_password; //声明变量
private RadioGroup sex;
private CheckBox hb_badminton, hb_basketball, hb_baseball;
private Intent intent;
private String muser;
private String mpassword;
private String msex;
private String mhobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
mhobby = new String();//存放多选框内容
}
private void init(){ //绑定控件
et_user = findViewById(R.id.et_user);
et_password = findViewById(R.id.et_password);
hb_badminton = findViewById(R.id.hb_badminton);
hb_baseball= findViewById(R.id.hb_baseball);
hb_basketball = findViewById(R.id.hb_basketball);
hb_badminton.setOnCheckedChangeListener(this);
hb_baseball.setOnCheckedChangeListener(this);
hb_basketball.setOnCheckedChangeListener(this);
sex = findViewById(R.id.sex);
//通过匿名内部类的形式为单选框注册监听
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//判断性别
if (checkedId==R.id.sex_man){
msex = "男";
}else {
msex = "女";
}
}
});
}
//通过实现接口的形式为多选框组注册监听
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String motion = buttonView.getText().toString();
if (isChecked){
if (!mhobby.contains(motion)){
mhobby = mhobby + motion;
}
}else {
if (mhobby.contains(motion)) {
mhobby = mhobby.replace(motion, "");
}
}
}
//通过注册按钮的click属性实现点击事件
public void onClick(View v) {
if (v.getId()==R.id.btn_login){
//获取用户名和密码
muser = et_user.getText().toString();
mpassword = et_password.getText().toString();
}
//通过Intent将用户名和密码、单选框内容和多选框内容,传递给secondActivity
intent = new Intent(this,SecondActivity.class);
intent.putExtra("muser",muser);
intent.putExtra("mpassword",mpassword);
intent.putExtra("msex",msex);
intent.putExtra("mhobby",mhobby);
startActivity(intent);
}
}
----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="浣犵殑淇℃伅涓猴細"
android:textSize="30sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:textColor="#000"/>
<TextView
android:id="@+id/tv_outData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="30dp"
android:text=""
android:textSize="30sp" />
</LinearLayout>
package com.example.show;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
private TextView tv_outData;//存放传递的数据
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//接受MainActivity传递的数据
Intent intent = getIntent();
String user = intent.getStringExtra("muser");
String password = intent.getStringExtra("mpassword");
String msex = intent.getStringExtra("msex");
String mhobby = intent.getStringExtra("mhobby");
tv_outData = findViewById(R.id.tv_outData);
tv_outData.setText("用户名:" + user + "\n" + "密 码:" + password + "\n" + "性 别:" + msex + "\n" + "爱 好:" + mhobby);
}
}
--------------------------------------------------------------------
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text=""
android:onClick="regist"/>
</RelativeLayout>
-------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="databack.RegistActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="74dp"
android:text="" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="33dp"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignLeft="@+id/textView1"
android:text="" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:onClick="click"
android:text="" />
</RelativeLayout>