activity_main.xml布局文件:
<?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" tools:context="com.mrzhao.day12reviewproject.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textColor="@android:color/black" android:textSize="20sp" /> <EditText android:id="@+id/userName_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" android:textColor="@android:color/black" android:textSize="20sp" /> <EditText android:id="@+id/userPassword_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <CheckBox android:id="@+id/rememberPassword_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="记住密码" android:textSize="20sp" /> <CheckBox android:id="@+id/autoLogin_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="自动登陆" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/login_bt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="20dp" android:layout_weight="1" android:onClick="onClick" android:text="登陆" android:textSize="20sp" tools:ignore="OnClick" /> <Button android:id="@+id/register_bt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="20dp" android:layout_weight="1" android:onClick="onClick" android:text="注册" android:textSize="20sp" tools:ignore="OnClick" /> </LinearLayout> </LinearLayout>
MainActivity文件:
public class MainActivity extends AppCompatActivity { private CheckBox rememberPasswordCb; private CheckBox autoLoginCb; private EditText userNameEt; private EditText userPasswordEt; private RegisterEntity registerEntity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userNameEt = (EditText) findViewById(R.id.userName_et); userPasswordEt = (EditText) findViewById(R.id.userPassword_et); rememberPasswordCb = (CheckBox) findViewById(R.id.rememberPassword_cb); autoLoginCb = (CheckBox) findViewById(R.id.autoLogin_cb); } public void onClick(View view) { switch (view.getId()) { case R.id.login_bt: String userName = userNameEt.getText().toString(); String userPassword = userPasswordEt.getText().toString(); String rememberCb = ""; String autoCb = ""; if (TextUtils.isEmpty(userName)) { Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(userPassword)) { Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show(); return; } //登陆 if (registerEntity == null) { Toast.makeText(this, "请注册", Toast.LENGTH_SHORT).show(); return; } if (rememberPasswordCb.isChecked()) { rememberCb = "选中了记住密码"; } else { rememberCb = "没选记住密码"; } if (autoLoginCb.isChecked()) { autoCb = "选中了自动登陆"; } else { autoCb = "没选自动登陆"; } //将 注册的对象 和 是否自动登陆 是否记住密码的信息 传递到首页面 Intent loginIntent = new Intent(this, HomeActivity.class); //传递注册对象 loginIntent.putExtra("registerEntity", registerEntity); loginIntent.putExtra("remember", rememberCb); loginIntent.putExtra("auto", autoCb); //启动 startActivity(loginIntent); break; case R.id.register_bt: //注册 Intent registerIntent = new Intent(this, RegisterActivity.class); //为了结果而 启动Activity startActivityForResult(registerIntent, 10000); break; } } /** * 用于接收注册页面的回传值 * * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //判断 请求吗和结果吗 if (requestCode == 10000 && resultCode == 10001) { String userName = data.getStringExtra("registerName"); String userPassword = data.getStringExtra("registerPassword"); String userSex = data.getStringExtra("registerSex"); String userAge = data.getStringExtra("registerAge"); ArrayList<String> hobbyList = data.getStringArrayListExtra("registerHobbyList"); //定义一个对象 registerEntity = new RegisterEntity(userName, userPassword, userAge, userSex, hobbyList); //设置展示到文本框中 userNameEt.setText(userName); userPasswordEt.setText(userPassword); Toast.makeText(this, userName + "\n" + userPassword + "\n" + userAge + "\n" + userSex + "\n" + hobbyList.toString(), Toast.LENGTH_SHORT).show(); } } }
register_layout.xml布局文件:
<?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" tools:context="com.mrzhao.day12reviewproject.RegisterActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textColor="@android:color/black" android:textSize="20sp" /> <EditText android:id="@+id/register_name_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" android:textColor="@android:color/black" android:textSize="20sp" /> <EditText android:id="@+id/register_password_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年 龄:" android:textColor="@android:color/black" android:textSize="20sp" /> <EditText android:id="@+id/register_age_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入年龄" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性 别:" android:textColor="@android:color/black" android:textSize="20sp" /> <RadioGroup android:id="@+id/register_sex_rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/register_sex_man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="男" android:textSize="20dp" /> <RadioButton android:id="@+id/register_sex_woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="女" android:textSize="20sp" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱 好:" android:textColor="@android:color/black" android:textSize="20sp" /> <RelativeLayout android:id="@+id/register_hobby_container" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打游戏" android:textSize="20sp" /> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/cb1" android:text="打台球" android:textSize="20sp" /> <CheckBox android:id="@+id/cb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/cb2" android:text="吃鸡" android:textSize="20sp" /> <CheckBox android:id="@+id/cb4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/cb1" android:text="爱学习" android:textSize="20sp" /> <CheckBox android:id="@+id/cb5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/cb1" android:layout_toRightOf="@id/cb4" android:text="爬山" android:textSize="20sp" /> <CheckBox android:id="@+id/cb6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/cb1" android:layout_toRightOf="@id/cb5" android:text="睡觉" android:textSize="20sp" /> <CheckBox android:id="@+id/cb7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/cb4" android:text="旅游" android:textSize="20sp" /> <CheckBox android:id="@+id/cb8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/cb4" android:layout_toRightOf="@id/cb7" android:text="百望山" android:textSize="20sp" /> </RelativeLayout> </LinearLayout> <Button android:onClick="onClick" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20sp" android:text="注册完成" android:textSize="20sp" tools:ignore="OnClick" /> </LinearLayout>
RegisterActivity文件:
public class RegisterActivity extends AppCompatActivity { private String registerName, registerPassword, registerAge, registerSex; private ArrayList<String> registerHobbyList; private EditText registerNameEt; private EditText registerPasswordEt; private EditText registerAgeEt; private RadioGroup registerSexRg; private RelativeLayout hobbyContainerRl; private CheckBox[] boxes; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); registerNameEt = (EditText) findViewById(R.id.register_name_et); registerPasswordEt = (EditText) findViewById(R.id.register_password_et); registerAgeEt = (EditText) findViewById(R.id.register_age_et); //性别 registerSexRg = (RadioGroup) findViewById(R.id.register_sex_rg); registerSexRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.register_sex_man: registerSex = "男"; break; case R.id.register_sex_woman: registerSex = "女"; break; } } }); //爱好 hobbyContainerRl = (RelativeLayout) findViewById(R.id.register_hobby_container); //定义一共有多少个爱好的 数组 boxes = new CheckBox[hobbyContainerRl.getChildCount()]; //实例化数组中的每个对象 for (int i = 0; i < boxes.length; i++) { boxes[i] = (CheckBox) hobbyContainerRl.getChildAt(i); } } public void onClick(View view) { //获取输入的值 registerName = registerNameEt.getText().toString(); registerPassword = registerPasswordEt.getText().toString(); registerAge = registerAgeEt.getText().toString(); if (TextUtils.isEmpty(registerName)) { Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(registerPassword)) { Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(registerAge)) { Toast.makeText(this, "请输入年龄", Toast.LENGTH_SHORT).show(); return; } //获取爱好 registerHobbyList = new ArrayList<>(); for (int i = 0; i < boxes.length; i++) { // 判断被选中的 CheckBox是哪个 if (boxes[i].isChecked()) { String hobby = boxes[i].getText().toString(); registerHobbyList.add(hobby); } } //已经获取了全部的注册信息 进行回传值 Intent intent = new Intent(); intent.putExtra("registerName", registerName); intent.putExtra("registerPassword", registerPassword); intent.putExtra("registerSex", registerSex); intent.putExtra("registerAge", registerAge); intent.putExtra("registerHobbyList", registerHobbyList); //设置结果 setResult(10001,intent); //提示 Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show(); //结束当前页面 finish(); } }
home_layout.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <ScrollView 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="com.mrzhao.day12reviewproject.HomeActivity"> <TextView android:layout_width="match_parent" android:textSize="20sp" android:layout_margin="20dp" android:id="@+id/show_tv" android:layout_height="match_parent" /> </ScrollView>
HomeActivity文件:
public class HomeActivity extends AppCompatActivity { private TextView showTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); Intent intent = getIntent(); //获取登陆注册的信息对象 RegisterEntity registerEntity = (RegisterEntity) intent.getSerializableExtra("registerEntity"); String remember = intent.getStringExtra("remember"); String auto = intent.getStringExtra("auto"); showTv = (TextView) findViewById(R.id.show_tv); //展示到文本上 showTv.setText(registerEntity.toString() + "\n" + remember + "\n" + auto); } }
RegisterEntity文件:
public class RegisterEntity implements Serializable{ private String name; private String password; private String age; private String sex; private ArrayList<String> hobbies; public RegisterEntity(String name, String password, String age, String sex, ArrayList<String> hobbies) { this.name = name; this.password = password; this.age = age; this.sex = sex; this.hobbies = hobbies; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public ArrayList<String> getHobbies() { return hobbies; } public void setHobbies(ArrayList<String> hobbies) { this.hobbies = hobbies; } @Override public String toString() { return "RegisterEntity{" + "name='" + name + '\'' + ", password='" + password + '\'' + ", age='" + age + '\'' + ", sex='" + sex + '\'' + ", hobbies=" + hobbies + '}'; } }