Android Studio製作簡單登錄界面

来源:https://www.cnblogs.com/tututut/p/18125357
-Advertisement-
Play Games

Android Studio製作簡單登錄界面 應用線性佈局設計登錄界面,要求點擊輸入學號時彈出數字鍵盤界面,點擊輸入密碼時彈出字母鍵盤,出現的文字、數字、尺寸等全部在values文件夾下相應.xml文件中設置好,使用時直接引用。當用戶名或密碼為空,顯示一個提示信息“用戶名與密碼不能為空!”,當用戶名... ...


實現目標

應用線性佈局設計登錄界面,要求點擊輸入學號時彈出數字鍵盤界面,點擊輸入密碼時彈出字母鍵盤,出現的文字、數字、尺寸等全部在values文件夾下相應.xml文件中設置好,使用時直接引用。當用戶名或密碼為空,顯示一個提示信息“用戶名與密碼不能為空!”,當用戶名和密碼匹配,顯示“登錄成功”。

效果圖如下:

實現過程

新建項目

新建一個項目如圖所示:

UI設計

1.新建login.xml,選擇線性佈局

步驟如下:

設計登錄頁面

LinearLayout是線性佈局,佈局中的組件按照垂直或者水平方向進行排列

gravity:設置自身內部元素的對齊方式

layout_gravity:用來控制該控制項在包含該控制項的父控制項中的位置

本設計採用垂直線性佈局,如圖所示:

控制項類型: EditText 是一個允許用戶輸入和編輯文本的控制項。

android:id: 這個屬性為控制項設置了一個唯一的ID(@+id/ed2),使得開發者可以在Java中通過這個ID來引用這個控制項。

android:layout_width 和 android:layout_height: 這些屬性定義了控制項的寬度和高度。531dp 指定了寬度為531設備獨立像素,wrap_content 表示高度會根據內容的大小自動調整。

實現代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="25dp"
    android:background="@color/white"
    tools:context="com.example.myapplication1.LoginActivity"
    android:orientation="vertical"
    android:weightSum="1">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login_page_title"
        android:textSize="@dimen/text_size_large"
        android:textColor="@android:color/black"
        android:layout_gravity="center_horizontal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="0.55">
        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="@dimen/label_width"
                android:layout_height="wrap_content"
                android:text="@string/student_id_label"
                android:textSize="@dimen/text_size_medium"
                android:textColor="@android:color/black"/>

            <EditText
                android:id="@+id/ed1"
                android:layout_width="531dp"
                android:layout_height="wrap_content"
                android:minHeight="48dp"
                android:padding="12dp"
                android:hint="@string/student_id_hint"
                android:inputType="number"
                android:textColor="@color/black"
                android:textColorHint="@android:color/darker_gray"
                android:visibility="visible" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="@dimen/label_width"
                android:layout_height="wrap_content"
                android:text="@string/password_label"
                android:textSize="@dimen/text_size_medium"
                android:textColor="@android:color/black"/>
            <EditText
                android:id="@+id/ed2"
                android:layout_width="531dp"
                android:layout_height="wrap_content"
                android:minHeight="48dp"
                android:padding="12dp"
                android:hint="@string/password_hint"
                android:inputType="text"
                android:textColor="@color/black"
                android:textColorHint="@android:color/darker_gray"
                android:visibility="visible" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:layout_width="@dimen/login_button_width"
        android:layout_height="wrap_content"
        android:text="@string/login_button_text"
        android:textSize="@dimen/text_size_button"
        android:id="@+id/bt"
        android:layout_gravity="center_horizontal" />
        
</LinearLayout>

2.將文本、數字和尺寸等資源從佈局文件中移動到values文件夾下的相應.xml文件中並引用,需要按照以下步驟操作:

文本(字元串)資源:在values文件夾下的strings.xml文件中定義。

尺寸資源:在values文件夾下的dimens.xml文件中定義。

顏色資源:已經在colors.xml中定義,可以繼續添加新的顏色或使用已有的顏色。

具體代碼如下:

strings.xml

<resources>
    <string name="login_page_title">登錄頁面</string>
    <string name="student_id_hint">請輸入學號</string>
    <string name="password_hint">請輸入密碼</string>
    <string name="student_id_label">學號:</string>
    <string name="password_label">密碼:</string>
    <string name="login_button_text">登錄</string>
</resources>

dimens.xml

<resources>
    <dimen name="text_size_large">30dp</dimen>
    <dimen name="text_size_medium">18dp</dimen>
    <dimen name="login_button_width">285dp</dimen>
    <dimen name="login_input_width">300dp</dimen>
    <dimen name="label_width">65dp</dimen>
    <dimen name="text_size_button">20dp</dimen>
</resources>

調用

1.新建一個LoginActivity進行調用,如圖所示:

定義一個登錄界面的行為:包含兩個文本輸入框(EditText)用於輸入用戶名和密碼,以及一個按鈕(Button)用於提交登錄信息。

成員變數:

  • usertext 和 passtext 是EditText類型的變數,分別用於獲取用戶輸入的用戶名和密碼。

onCreate方法:

  • 在onCreate方法中,首先調用super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)來初始化界面。

ButtonListener 類:

  • ButtonListener實現了View.OnClickListener介面,用於處理按鈕點擊事件。

  • 在其onClick方法中,首先獲取usertext和passtext中的文本內容。

  • 然後,通過一系列的條件判斷,檢查用戶名和密碼是否為空,是否匹配預設的正確用戶名("2021")和密碼("abc")。

  • 如果用戶名或密碼為空,顯示一個提示信息“用戶名與密碼不能為空!”。

  • 如果用戶名和密碼匹配,顯示“登錄成功”。

具體實現代碼如下:

package com.example.myapplication1;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
        private EditText usertext;
        private EditText passtext;
        private Button loginbutton;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
            usertext=(EditText)this.findViewById(R.id.ed1);
            passtext=(EditText)this.findViewById(R.id.ed2);
            loginbutton=(Button)this.findViewById(R.id.bt);
            loginbutton.setOnClickListener(new ButtonListener());
        }
        private class ButtonListener implements View.OnClickListener{
            @Override
            public void onClick(View v){
                String user=usertext.getText().toString();
                String pass=passtext.getText().toString();
                if (user.equals("")||pass.equals("")){
                    Toast.makeText(LoginActivity.this,"用戶名與密碼不能為空!",Toast.LENGTH_SHORT).show();
                }
                else if (user.equals("2021")&&pass.equals("abc")){
                    Toast.makeText(LoginActivity.this,"登陸成功",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(LoginActivity.this,"用戶名或密碼輸入有誤,請更正後重新輸入!",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

配置文件

AndroidManifest.xml是整個Android應用程式的全局面描述配置文件

清單文件中通常包含以下六項信息:

  • 聲明應用程式的包名: 用來識別應用程式的唯一標誌

  • 描述應用程式組件

  • 確定宿主應用組件進程

  • 聲明應用程式擁有的許可權

  • 定義應用程式所支持API的最低等級

  • 列舉應用程式必須鏈接的庫

添加LoginActivity到 AndroidManifest.xml中

具體代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat"
        tools:targetApi="31">
        <activity
            android:name=".LoginActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

總結

以上就是簡單的登錄界面的設計的所有內容,簡單介紹了線性佈局以及相應屬性的應用。

如果這篇文章對你或你的朋友有幫助的話,請多多支持和分享,讓更多的人受益。同時,如果你有任何問題或疑問,也歡迎在下方留言,我會儘快回覆並幫助你解決問題。讓我們一起共同進步,共同學習!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在實際項目中,從Kafka到HDFS的數據是每天自動生成一個文件,按日期區分。而且Kafka在不斷生產數據,因此看看kettle是不是需要時刻運行?能不能按照每日自動生成數據文件? 為了測試實際項目中的海豚定時調度從Kafka到HDFS的Kettle任務情況,特地提前跑一下海豚定時調度這個任務,看看 ...
  • 在Centos7中使用的包管理工具是yum,當然使用包管理工具安裝也是最方便的。 本文操作內容需要在root用戶下,否則有些步驟無法成功執行。 系統環境信息展示 安裝 MySQL 提供的 RPM wget https://dev.mysql.com/get/mysql80-community-rel ...
  • 本文介紹瞭如何結合LFU淘汰策略與訪問頻率優化,實現在電商平臺等業務場景下,精準管理Redis中20萬熱點數據。 ...
  • 主從延遲調優思路 1、什麼是主從延遲? 本質是從庫的回放跟不上主庫,回放階段的延遲 2、主從延遲常見的原因有哪些? 1、大事務,從庫回放時間較長,導致主從延遲 2、主庫寫入過於頻繁,從庫回放跟不上 3、參數配置不合理 4、主從硬體差異 5、網路延遲 6、表沒有主鍵或者索引大量頻繁的更新 7、一些讀寫 ...
  • 我們討論面試中各大廠的SQL演算法面試題,往往核心考點就在於視窗函數,所以掌握好了視窗函數,面對SQL演算法面試往往事半功倍。 ...
  • MySQL安裝 官方: https://www.mysql.com/ MySQL官方提供了兩種不同的版本: 社區版本(MySQL Community Server) 免費, MySQL不提供任何技術支持 商業版本(MySQL Enterprise Edition) 收費,可以使用30天,官方提供技術 ...
  • 本次按照目前最新版本Sqlserver2022進行記錄 先決條件 任何受支持的 Linux 發行版上的 Docker 引擎 1.8 及更高版本。 有關詳細信息,請參閱 Install Docker(安裝 Docker)。 有關硬體要求和處理器支持的詳細信息,請參閱SQL Server 2022:硬體 ...
  • 運算符用於執行程式代碼運算,會針對一個以上操作數項目來進行運算。 考慮以下計算: 7 + 5 = 12 以上實例中 7、5 和 12 是操作數。 運算符 + 用於加值。 運算符 = 用於賦值。 TypeScript 主要包含以下幾種運算: 算術運算符 邏輯運算符 關係運算符 按位運算符 賦值運算符 ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...