Android登录界面的注册功能实现

注册一个登录界面在控制台将输入的信息文本选框展示出来

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"
    tools:context=".MainActivity"
    android:orientation="vertical"

    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:textSize="35sp"
        android:gravity="center"
        android:background="#FF5722"
        />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="210dp"
    android:orientation="horizontal"
    >
 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/qq"
     android:layout_weight="1"
     android:text="用QQ注册"
     android:gravity="center"
     android:textSize="20sp"
     />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="210dp"
        android:background="@drawable/wechat"
        android:layout_weight="1"
        android:text="用微信注册"
        android:textSize="20sp"
        />
</LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <ImageView
            android:id="@+id/yxlg"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="80dp"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/net"
            />
   <TextView
       android:paddingTop="5dp"
       android:paddingBottom="5dp"
       android:id="@+id/yxld"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="   用邮箱登录"
       android:gravity="center"
       android:textSize="35sp"
       />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:paddingTop="12dp"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text="名字:"
        android:gravity="left"
        android:textSize="25sp"
        />
    <EditText
        android:id="@+id/mz"
        android:paddingTop="12dp"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"

        />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        >
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:paddingTop="12dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="账号:"
            android:gravity="left"
            android:textSize="25sp"
            />
        <EditText
            android:id="@+id/zh"
            android:paddingTop="12dp"
            android:background="@null"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:paddingTop="12dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="密码:"
            android:gravity="left"
            android:textSize="25sp"
            />
        <EditText
            android:password="true"
            android:id="@+id/mm"
            android:paddingTop="12dp"
            android:background="@null"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:paddingTop="12dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="性别:        "
            android:gravity="left"
            android:textSize="25sp"
            />
        <RadioGroup
            android:id="@+id/xb"
            android:layout_marginTop="15dp"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/nan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="20sp"
            />
<RadioButton
    android:id="@+id/nu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="女"
    android:textSize="20sp"
    />
        </RadioGroup>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:paddingTop="12dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="选择你的爱好:"
            android:gravity="left"
            android:textSize="25sp"
            />
        <CheckBox
        android:id="@+id/cg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="唱歌"
        android:textSize="20sp" />
        <CheckBox
            android:id="@+id/tw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳舞"
            android:textSize="20sp" />
        <CheckBox
            android:id="@+id/ds"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="读书"
            android:textSize="20sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        >
    </LinearLayout>
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="提交"
        android:textSize="30sp"
        />
</LinearLayout>

注册功能实现

package com.example.registerlogin;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{
private EditText mz,zh,mm;
private Button btn1;
private String name,id,pwd,sex,hobby;
private RadioGroup xb;
private CheckBox cg,tw,ds;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=findViewById(R.id.btn1);
        mz=findViewById(R.id.mz);
        zh=findViewById(R.id.mz);
        mm=findViewById(R.id.mz);
        cg=findViewById(R.id.cg);
        tw=findViewById(R.id.tw);
        ds=findViewById(R.id.ds);
        xb=findViewById(R.id.xb);

        btn1.setOnClickListener(MainActivity.this);
        xb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                {switch (checkedId){
                    case R.id.nan:
                    sex = "男";
                    break;
                    case R.id.nu:
                    sex="女";
                    break;

                }
                }
            }
        });
    }
    private void getData(){
        name=mz.getText().toString().trim();
        id=zh.getText().toString().trim();
        pwd=mm.getText().toString().trim();
    }
     public void onClick(View v) {
         switch (v.getId()) {
             case R.id.btn1:
                 getData();

                 if (TextUtils.isEmpty(name)) {

                     Toast.makeText(MainActivity.this, "请输入名字", Toast.LENGTH_SHORT).show();
                 } else if (TextUtils.isEmpty(id)) {

                     Toast.makeText(MainActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                 } else if (TextUtils.isEmpty(pwd)) {

                     Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                 } else if (TextUtils.isEmpty(sex)) {

                     Toast.makeText(MainActivity.this, "请输入性别", Toast.LENGTH_SHORT).show();
                 }else if (TextUtils.isEmpty(hobby)) {

                     Toast.makeText(MainActivity.this, "请输入爱好", Toast.LENGTH_SHORT).show();
                 }else {
                     Toast.makeText(MainActivity.this, "注册成功", Log.i("MainActivity", "检测到你的注册信息:" + "名字:" + name + "  邮箱:" + id + "  性别:" + sex+"  爱好:"+hobby));
                 }
                 break;
         }
     }

    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
        String motion =buttonView.getText().toString();
        if(isChecked){
            if(!hobby.contains(motion)){
                hobby = hobby + motion;
            }
        }else {
            if(hobby.contains(motion)){

              hobby=hobby.replace(motion,"");
            }

        }
}

}

我没有输入爱好,所以控制台输出null

 这个代码较简单,xml用到的都是一些简单的线性布局,和一些对控件位置和id的改动,定义,Java文件用的是对xml控件的获取和输出,展示!如果有不明白的请留言,看到会第一时间回复的哦。

猜你喜欢

转载自blog.csdn.net/Abtxr/article/details/123751677