網站首頁 編程語言 正文
本文實例為大家分享了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控件的獲取和輸出,展示!
原文鏈接:https://blog.csdn.net/Abtxr/article/details/123751677
相關推薦
- 2022-11-02 一文搞懂Golang中的內存逃逸_Golang
- 2022-07-26 ubuntu18.04+cuda10.2+tensorrt8.4.1.5配置安裝
- 2022-08-18 Android?Canva實現漸變進度條_Android
- 2022-11-19 通過?C#/VB.NET?代碼將?Excel?工作表拆分為單獨的文件_C#教程
- 2022-06-29 RedisTemplate常用操作方法總結(set、hash、list、string等)_Redis
- 2022-07-22 關于不定方程解的個數的問題
- 2022-09-09 使用python怎樣產生10個不同的隨機數_python
- 2022-11-14 Git暫存區的意義或git add的意義
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支