網站首頁 編程語言 正文
想要制作一個簡易的登錄界面非常容易,總體上來說就是UI布局、給定id、新建跳轉的頁面、以及輸入賬號密碼的獲取與判斷,那么接下來就開始制作吧!
1.首先就是Activity中的組件布局,這個就不一一列舉了!自己把兩個EditText和一個Button擺好就ok了,像按鈕的點擊效果可以自己設計一下(默認狀態是什么顏色,按下去是什么顏色)。
2.再一個就是要給定控件一個id
<?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:background="@drawable/img_1" ? ? android:orientation="vertical"> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:orientation="vertical"> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="300dp" ? ? ? ? ? ? android:layout_marginTop="160dp" ? ? ? ? ? ? android:orientation="vertical" ? ? ? ? ? ? android:padding="30dp" ? ? ? ? ? ? android:gravity="center"> ? ? ? ? ? ? <EditText ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="60dp" ? ? ? ? ? ? ? ? android:id="@+id/EDit_username" ? ? ? ? ? ? ? ? android:hint="賬戶名" ? ? ? ? ? ? ? ? android:maxLines="1" ? ? ? ? ? ? ? ? android:textColor="#000000"/> ? ? ? ? ? ? ? <EditText ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="60dp" ? ? ? ? ? ? ? ? android:id="@+id/EDit_password" ? ? ? ? ? ? ? ? android:layout_marginTop="15dp" ? ? ? ? ? ? ? ? android:hint="賬戶名" ? ? ? ? ? ? ? ? android:maxLines="1" ? ? ? ? ? ? ? ? android:textColor="#000000"/> ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:layout_width="200dp" ? ? ? ? ? ? ? ? android:layout_height="60dp" ? ? ? ? ? ? ? ? android:layout_marginTop="30dp" ? ? ? ? ? ? ? ? android:id="@+id/btn_login" ? ? ? ? ? ? ? ? android:text="登錄" ? ? ? ? ? ? ? ? android:backgroundTint="@color/btn_xiaoguo" ? ? ? ? ? ? ? ? android:textSize="20sp"/> ? ? ? ? </LinearLayout> ? </LinearLayout> </LinearLayout>
3.然后就是要在Mainactivity.java中寫代碼了,需要申明控件id,綁定控件id及登錄按鈕的點擊事件(判斷是否是自己設定的密碼,判斷是否達到一定的長度)。 ?對了,還有需要定義存賬號密碼的類型名稱。
package com.example.denlu;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
?
public class MainActivity extends AppCompatActivity {
?
? ? private EditText mEDit_password; ? ? ?
? ? private EditText mEDit_username;
? ? private Button mbtn_login;
? ? private String zhanhao; ?//申明存入賬號的變量
? ? private String mima; ? //申明存入密碼的變量
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
?
? ? ? ? mEDit_username = findViewById(R.id.EDit_username); ? //綁定賬號Edit Text的id
? ? ? ? mEDit_password = findViewById(R.id.EDit_password); ?//綁定密碼Edit Text的id
? ? ? ? mbtn_login = findViewById(R.id.btn_login); ? //綁定按鈕Button的id
4.好了,現在要做的就是寫按鈕的點擊事件了;那么在這之前需要先新建一個跳轉之后的界面。之前也發過新建一個Activity的方法。
5.然后寫點擊事件;那么點擊事件要怎么寫,首先肯定是要把賬號與密碼都提取出來存入自定義的String變量,需要用到 ?.getText().toString() ?這兩個函數;既然提取出來了那么下一步就好辦了,直接用幾個if else if ?寫幾個判斷即可。
package com.example.denlu;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
?
public class MainActivity extends AppCompatActivity {
?
? ? private EditText mEDit_password;
? ? private EditText mEDit_username;
? ? private Button mbtn_login;
? ? private String zhanghao; //申明存入賬號的變量
? ? private String mima; ? //申明存入密碼的變量
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
?
? ? ? ? mEDit_username = findViewById(R.id.EDit_username); ? ? //綁定賬號Edit Text的id
? ? ? ? mEDit_password = findViewById(R.id.EDit_password); ? ? //綁定密碼Edit Text的id
? ? ? ? mbtn_login = findViewById(R.id.btn_login); ? ? ? ? ? ? //綁定按鈕Button的id
? ? ? ? mbtn_login.setOnClickListener(new View.OnClickListener() {
? ? ? ? ?@Override
? ? ? ? ?public void onClick(View view) {
? ? ? ? ?zhanghao = mEDit_username.getText().toString(); ? //將賬號取出來存入自定義的zhanhao變量
? ? ? ? ? ? ? ? mima = mEDit_password.getText().toString(); ? ? ? //將密碼取出來存入自定義的mima變量
? ? ? ? ? ? ? ? if (zhanghao.length()<3||zhanghao.length()>7){ ? ?//if判斷輸入賬號的長度是不是在3-7位數之間,如果不是則彈窗提示
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "賬號長度應為3-7位數之間", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }else if (mima.length()<6||mima.length()>6){ ? ? //if判斷輸入賬號的長度是不是6位數,如果不是則彈窗提示
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"請輸入6位數的密碼",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (zhanghao.equals("abcdef")&&mima.equals("123456")){ ? ? //如果輸入的賬號密碼是“abcdef” ?“123456” 則實行頁面跳轉
? ? ? ? ? ? ? ? ? ? Intent intent = new Intent(MainActivity.this,dengluMainActivity.class);
? ? ? ? ? ? ? ? ? ? startActivity(intent);
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"賬號或密碼輸入錯誤",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
嗯!就是這樣了,可能有些我沒注意講到,但是大概就是這樣了!
原文鏈接:https://blog.csdn.net/x97666/article/details/120583579
相關推薦
- 2023-01-28 C#實現XML文件操作詳解_C#教程
- 2022-10-08 C語言深入分析浮點型數據存儲_C 語言
- 2022-04-28 Pytorch中torch.flatten()和torch.nn.Flatten()實例詳解_pyt
- 2022-07-06 C++如何切割String對象的方法_C 語言
- 2022-04-09 Webservice 服務請求參數xml 嵌套問題/CDATA嵌套
- 2021-12-11 C++嵌入式內存管理詳情_C 語言
- 2022-11-06 Android?菜單欄DIY實現效果詳解_Android
- 2022-04-06 Go?字符串比較的實現示例_Golang
- 最近更新
-
- 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同步修改后的遠程分支