日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android?Studio實現簡易登錄界面制作_Android

作者:x97666 ? 更新時間: 2022-06-21 編程語言

想要制作一個簡易的登錄界面非常容易,總體上來說就是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

欄目分類
最近更新