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
  • 概述:本文代碼示例演示瞭如何在WPF中使用LiveCharts庫創建動態條形圖。通過創建數據模型、ViewModel和在XAML中使用`CartesianChart`控制項,你可以輕鬆實現圖表的數據綁定和動態更新。我將通過清晰的步驟指南包括詳細的中文註釋,幫助你快速理解並應用這一功能。 先上效果: 在 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • 概述:本示例演示了在WPF應用程式中實現多語言支持的詳細步驟。通過資源字典和數據綁定,以及使用語言管理器類,應用程式能夠在運行時動態切換語言。這種方法使得多語言支持更加靈活,便於維護,同時提供清晰的代碼結構。 在WPF中實現多語言的一種常見方法是使用資源字典和數據綁定。以下是一個詳細的步驟和示例源代 ...
  • 描述(做一個簡單的記錄): 事件(event)的本質是一個委托;(聲明一個事件: public event TestDelegate eventTest;) 委托(delegate)可以理解為一個符合某種簽名的方法類型;比如:TestDelegate委托的返回數據類型為string,參數為 int和 ...
  • 1、AOT適合場景 Aot適合工具類型的項目使用,優點禁止反編 ,第一次啟動快,業務型項目或者反射多的項目不適合用AOT AOT更新記錄: 實實在在經過實踐的AOT ORM 5.1.4.117 +支持AOT 5.1.4.123 +支持CodeFirst和非同步方法 5.1.4.129-preview1 ...
  • 總說周知,UWP 是運行在沙盒裡面的,所有許可權都有嚴格限制,和沙盒外交互也需要特殊的通道,所以從根本杜絕了 UWP 毒瘤的存在。但是實際上 UWP 只是一個應用模型,本身是沒有什麼許可權管理的,許可權管理全靠 App Container 沙盒控制,如果我們脫離了這個沙盒,UWP 就會放飛自我了。那麼有沒... ...
  • 目錄條款17:讓介面容易被正確使用,不易被誤用(Make interfaces easy to use correctly and hard to use incorrectly)限制類型和值規定能做和不能做的事提供行為一致的介面條款19:設計class猶如設計type(Treat class de ...
  • title: 從零開始:Django項目的創建與配置指南 date: 2024/5/2 18:29:33 updated: 2024/5/2 18:29:33 categories: 後端開發 tags: Django WebDev Python ORM Security Deployment Op ...
  • 1、BOM對象 BOM:Broswer object model,即瀏覽器提供我們開發者在javascript用於操作瀏覽器的對象。 1.1、window對象 視窗方法 // BOM Browser object model 瀏覽器對象模型 // js中最大的一個對象.整個瀏覽器視窗出現的所有東西都 ...