Android Studio實(shí)現(xiàn)簡(jiǎn)易登錄界面制作
想要制作一個(gè)簡(jiǎn)易的登錄界面非常容易,總體上來說就是UI布局、給定id、新建跳轉(zhuǎn)的頁面、以及輸入賬號(hào)密碼的獲取與判斷,那么接下來就開始制作吧!
1.首先就是Activity中的組件布局,這個(gè)就不一一列舉了!自己把兩個(gè)EditText和一個(gè)Button擺好就ok了,像按鈕的點(diǎn)擊效果可以自己設(shè)計(jì)一下(默認(rèn)狀態(tài)是什么顏色,按下去是什么顏色)。
2.再一個(gè)就是要給定控件一個(gè)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及登錄按鈕的點(diǎn)擊事件(判斷是否是自己設(shè)定的密碼,判斷是否達(dá)到一定的長(zhǎng)度)。 對(duì)了,還有需要定義存賬號(hào)密碼的類型名稱。
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; ?//申明存入賬號(hào)的變量 ? ? private String mima; ? //申明存入密碼的變量 ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? ? mEDit_username = findViewById(R.id.EDit_username); ? //綁定賬號(hào)Edit Text的id ? ? ? ? mEDit_password = findViewById(R.id.EDit_password); ?//綁定密碼Edit Text的id ? ? ? ? mbtn_login = findViewById(R.id.btn_login); ? //綁定按鈕Button的id
4.好了,現(xiàn)在要做的就是寫按鈕的點(diǎn)擊事件了;那么在這之前需要先新建一個(gè)跳轉(zhuǎn)之后的界面。之前也發(fā)過新建一個(gè)Activity的方法。
5.然后寫點(diǎn)擊事件;那么點(diǎn)擊事件要怎么寫,首先肯定是要把賬號(hào)與密碼都提取出來存入自定義的String變量,需要用到 .getText().toString() 這兩個(gè)函數(shù);既然提取出來了那么下一步就好辦了,直接用幾個(gè)if else if 寫幾個(gè)判斷即可。
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; //申明存入賬號(hào)的變量 ? ? private String mima; ? //申明存入密碼的變量 ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? ? mEDit_username = findViewById(R.id.EDit_username); ? ? //綁定賬號(hào)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(); ? //將賬號(hào)取出來存入自定義的zhanhao變量 ? ? ? ? ? ? ? ? mima = mEDit_password.getText().toString(); ? ? ? //將密碼取出來存入自定義的mima變量 ? ? ? ? ? ? ? ? if (zhanghao.length()<3||zhanghao.length()>7){ ? ?//if判斷輸入賬號(hào)的長(zhǎng)度是不是在3-7位數(shù)之間,如果不是則彈窗提示 ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "賬號(hào)長(zhǎng)度應(yīng)為3-7位數(shù)之間", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? }else if (mima.length()<6||mima.length()>6){ ? ? //if判斷輸入賬號(hào)的長(zhǎng)度是不是6位數(shù),如果不是則彈窗提示 ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"請(qǐng)輸入6位數(shù)的密碼",Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (zhanghao.equals("abcdef")&&mima.equals("123456")){ ? ? //如果輸入的賬號(hào)密碼是“abcdef” ?“123456” 則實(shí)行頁面跳轉(zhuǎn) ? ? ? ? ? ? ? ? ? ? Intent intent = new Intent(MainActivity.this,dengluMainActivity.class); ? ? ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"賬號(hào)或密碼輸入錯(cuò)誤",Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
嗯!就是這樣了,可能有些我沒注意講到,但是大概就是這樣了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android?Studio實(shí)現(xiàn)登錄界面功能
- Android?studio?利用共享存儲(chǔ)進(jìn)行用戶的注冊(cè)和登錄驗(yàn)證功能
- Android Studio實(shí)現(xiàn)登錄功能案例講解
- Android Studio實(shí)現(xiàn)QQ的注冊(cè)登錄和好友列表跳轉(zhuǎn)
- Android Studio+Servlet+MySql實(shí)現(xiàn)登錄注冊(cè)
- Android Studio連接MySql實(shí)現(xiàn)登錄注冊(cè)(附源代碼)
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊(cè)實(shí)現(xiàn)
- Android Studio實(shí)現(xiàn)簡(jiǎn)單的QQ登錄界面的示例代碼
- Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫的使用流程
- Android?studio實(shí)現(xiàn)app登錄界面
相關(guān)文章
android上實(shí)現(xiàn)0.5px線條的原理分析
這篇文章主要介紹了android上實(shí)現(xiàn)0.5px線條的原理分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Android 軟鍵盤彈出時(shí)把原來布局頂上去的解決方法
本文主要介紹了Android軟鍵盤彈出時(shí)把原來布局頂上去的解決方法。具有一定的參考作用,下面跟著小編一起來看下吧2017-01-01Android Rreact Native 常見錯(cuò)誤總結(jié)
這篇文章主要介紹了Android Rreact Native 常見錯(cuò)誤總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-06-06Android如何實(shí)現(xiàn)鎖屏狀態(tài)下彈窗
在鎖屏狀態(tài)下彈窗的效果我們平時(shí)并不少見,如QQ、微信和鬧鐘等,但是Android開發(fā)者要怎么實(shí)現(xiàn)這一功能呢?下面一起來看看。2016-08-08Android中ListView異步加載圖片錯(cuò)位、重復(fù)、閃爍問題分析及解決方案
在Android所有系統(tǒng)自帶的控件當(dāng)中,ListView這個(gè)控件算是用法比較復(fù)雜的了,關(guān)鍵是用法復(fù)雜也就算了,它還經(jīng)常會(huì)出現(xiàn)一些稀奇古怪的問題,讓人非常頭疼,下面通過本篇文章給大家分享Android中ListView異步加載圖片錯(cuò)位、重復(fù)、閃爍問題分析及解決方案,需要朋友可以參考2015-08-08利用Android中BitmapShader制作自帶邊框的圓形頭像
這篇文章給大家介紹了一下如何利用BitmapShader制作圓形頭像,可以自定義要顯示的圖片,邊框顏色和邊框?qū)挾鹊龋行枰呐笥褌兛梢詤⒖冀梃b。2016-09-09