Android Studio實現(xiàn)簡易登錄界面制作
想要制作一個簡易的登錄界面非常容易,總體上來說就是UI布局、給定id、新建跳轉的頁面、以及輸入賬號密碼的獲取與判斷,那么接下來就開始制作吧!

1.首先就是Activity中的組件布局,這個就不一一列舉了!自己把兩個EditText和一個Button擺好就ok了,像按鈕的點擊效果可以自己設計一下(默認狀態(tài)是什么顏色,按下去是什么顏色)。

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的id4.好了,現(xiàn)在要做的就是寫按鈕的點擊事件了;那么在這之前需要先新建一個跳轉之后的界面。之前也發(fā)過新建一個Activity的方法。

5.然后寫點擊事件;那么點擊事件要怎么寫,首先肯定是要把賬號與密碼都提取出來存入自定義的String變量,需要用到 .getText().toString() 這兩個函數(shù);既然提取出來了那么下一步就好辦了,直接用幾個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位數(shù)之間,如果不是則彈窗提示
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "賬號長度應為3-7位數(shù)之間", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }else if (mima.length()<6||mima.length()>6){ ? ? //if判斷輸入賬號的長度是不是6位數(shù),如果不是則彈窗提示
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"請輸入6位數(shù)的密碼",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();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
}嗯!就是這樣了,可能有些我沒注意講到,但是大概就是這樣了!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android?Studio實現(xiàn)登錄界面功能
- Android?studio?利用共享存儲進行用戶的注冊和登錄驗證功能
- Android Studio實現(xiàn)登錄功能案例講解
- Android Studio實現(xiàn)QQ的注冊登錄和好友列表跳轉
- Android Studio+Servlet+MySql實現(xiàn)登錄注冊
- Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實現(xiàn)
- Android Studio實現(xiàn)簡單的QQ登錄界面的示例代碼
- Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫的使用流程
- Android?studio實現(xiàn)app登錄界面
相關文章
Android編程實現(xiàn)仿優(yōu)酷旋轉菜單效果(附demo源碼)
這篇文章主要介紹了Android編程實現(xiàn)仿優(yōu)酷旋轉菜單效果的方法,較為詳細的分析了Android實現(xiàn)旋轉菜單的布局與功能實現(xiàn)技巧,并附帶完整的demo源碼供讀者下載參考,需要的朋友可以參考下2015-12-12
Android自定義View之漸變色折線圖的實現(xiàn)
折線圖的實現(xiàn)方法在github上有很多開源的程序,但是對于初學者來講,簡單一點的教程可能更容易入門,下面這篇文章主要給大家介紹了關于Android自定義View之漸變色折線圖的相關資料,需要的朋友可以參考下2022-04-04
Android App中ViewPager與Fragment結合的一些問題解決
這篇文章主要介紹了Android App中ViewPager與Fragment結合的一些問題解決,重點講解了如何更新及替換ViewPager中的Fragment,需要的朋友可以參考下2016-03-03
Android開發(fā)使用Messenger及Handler進行通信的方法示例
這篇文章主要介紹了Android開發(fā)使用Messenger及Handler進行通信的方法,結合實例形式分析了Android使用Messenger及Handler定義客戶端與服務器端實現(xiàn)通信的相關操作技巧,需要的朋友可以參考下2017-12-12

