登录与注册案例练习

效果图:



代码:

布局文件: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:gravity="center"
    android:orientation="vertical"
    tools:context="com.mrzhao.day1prectisproject.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/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">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/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">

        <Button
            android:id="@+id/login_bt"
            android:onClick="onClick"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登录" />

        <Button
            android:id="@+id/register_bt"
            android:onClick="onClick"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="注册" />

    </LinearLayout>
</LinearLayout>

activity_register.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:gravity="center"
    android:orientation="vertical"
    tools:context="com.mrzhao.day1prectisproject.RegisterActivity">

    <LinearLayout
        android:layout_width="match_parent"
        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/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:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码:"
            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:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年    龄:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/register_age_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入年龄"
            android:inputType="phone" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性    别:"
            android:textSize="20sp" />

        <RadioGroup
            android:id="@+id/register_radioGroup"
            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:text="" />

            <RadioButton
                android:id="@+id/register_sex_woman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="" />

        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="爱    好:"
            android:textSize="20sp" />

        <RelativeLayout
            android:id="@+id/hobby_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <CheckBox
                android:id="@+id/cb_game"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打游戏" />

            <CheckBox
                android:id="@+id/cb_sleep"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/cb_game"
                android:text="睡觉" />

            <CheckBox
                android:id="@+id/cb_sing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/cb_game"
                android:text="唱歌" />

            <CheckBox
                android:id="@+id/cb_beauty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/cb_sing"
                android:text="美颜" />

            <CheckBox
                android:id="@+id/cb_baskteball"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/cb_game"
                android:layout_toRightOf="@id/cb_sleep"
                android:text="打篮球" />

            <CheckBox
                android:id="@+id/cb_hua"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/cb_game"
                android:layout_toRightOf="@id/cb_baskteball"
                android:text="轮滑" />


        </RelativeLayout>
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="确认" />

</LinearLayout>

Java代码:

MainActivity:

public class MainActivity extends AppCompatActivity {

    private EditText nameEt;
    private EditText passwordEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameEt = (EditText) findViewById(R.id.name_et);
        passwordEt = (EditText) findViewById(R.id.password_et);
    }

    /**
     * 监听点击事件
     * @param view
     */
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_bt:
                //登录
                String name = nameEt.getText().toString();
                String password = passwordEt.getText().toString();

            //跳转到首页
                Intent intent1= new Intent(this,HomeActivity.class);
                intent1.putExtra("info",name+password);
                startActivity(intent1);


                break;
            case R.id.register_bt:
                //注册  跳转到注册页面
                Intent intent = new Intent(this,RegisterActivity.class);

                intent.putExtra("info","我是传递过来的数据");
                Student student = new Student("柴伟", "张磊");
                intent.putExtra("student",student);
                startActivityForResult(intent,1000);
                break;
        }
    }

    /**
     * 用于接收回传值的方法
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 1000&&resultCode ==1001){
                String name = data.getStringExtra("name");
                String password = data.getStringExtra("password");
                String age = data.getStringExtra("age");
                String sex = data.getStringExtra("sex");
                String hobby = data.getStringExtra("hobby");
                Toast.makeText(this,name+password+age+sex+hobby, Toast.LENGTH_SHORT).show();
            }
    }
}

RegisterActivity:

public class RegisterActivity extends AppCompatActivity {

    private String userName, userPassword, userAge, userSex, userHobby;
    private EditText userNameEt;
    private EditText userPasswordEt;
    private EditText userAgeEt;
    private RadioGroup radioGroup;
    private RelativeLayout hobbyLayout;
    private CheckBox[] boxes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //获取上个页面传递过来的数据
        Intent intent = getIntent();
        String info = intent.getStringExtra("info");
        Student student = (Student) intent.getSerializableExtra("student");
        Toast.makeText(this, info+student.toString(), Toast.LENGTH_SHORT).show();


        userNameEt = (EditText) findViewById(R.id.register_name_et);
        userPasswordEt = (EditText) findViewById(R.id.register_password_et);
        userAgeEt = (EditText) findViewById(R.id.register_age_et);
        hobbyLayout = (RelativeLayout) findViewById(R.id.hobby_layout);

        int count = hobbyLayout.getChildCount();
        boxes = new CheckBox[count];
        //实例化checkBox
        for (int i = 0; i < count; i++) {
            boxes[i] = (CheckBox) hobbyLayout.getChildAt(i);
        }


        radioGroup = (RadioGroup) findViewById(R.id.register_radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.register_sex_man:
                        userSex = "";
                        break;
                    case R.id.register_sex_woman:
                        userSex = "";
                        break;
                }
            }
        });
    }

    //注册完成
    public void onClick(View view) {
        //获取输入的用户名
        userName = userNameEt.getText().toString();
        //获取输入的密码
        userPassword = userPasswordEt.getText().toString();
        // 获取输入的 年龄
        userAge = userAgeEt.getText().toString();
        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 (TextUtils.isEmpty(userAge)) {
            Toast.makeText(this, "请输入年龄", Toast.LENGTH_SHORT).show();
            return;
        }

        //获取用户注册的兴趣
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < boxes.length; i++) {
            if (boxes[i].isChecked()) {
                String hobby = boxes[i].getText().toString();
                buffer.append(hobby + " ");
            }
        }
        userHobby = buffer.toString();

        //回传值
        Intent  intent = new Intent();
        intent.putExtra("name",userName);
        intent.putExtra("password",userPassword);
        intent.putExtra("age",userAge);
        intent.putExtra("sex",userSex);
        intent.putExtra("hobby",userHobby);
        //设置回传值
        setResult(1001,intent);
        //关闭当前的activity
        finish();
    }


}

HomeActivity:

public class HomeActivity extends AppCompatActivity {

    private TextView showTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        showTv = (TextView) findViewById(R.id.show_tv);
        Intent intent = getIntent();
        String info = intent.getStringExtra("info");
        showTv.setText(info);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值