網站首頁 編程語言 正文
本文實例為大家分享了Android Studio實現登錄界面的具體代碼,供大家參考,具體內容如下
題目
設計一個登錄界面。要求:
a) 包含用戶名、密碼、記住密碼、“忘記密碼”按鈕和“登錄”按鈕。
b) 單擊“忘記密碼”按鈕彈出提示對話框,對話框內容自擬。
答案
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? android:id="@+id/activity_login" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? > <RelativeLayout android:id="@+id/login_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:focusable="true" android:focusableInTouchMode="true" ? ? > <RelativeLayout ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" ? ? android:layout_below="@+id/login_edit_pwd" ? ? android:layout_marginTop="20dp" ? ? android:layout_marginBottom="20dp"> ? ? <Button ? ? ? ? android:id="@+id/login_btn_login" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? android:layout_marginTop="52dp" ? ? ? ? android:layout_marginRight="50dp" ? ? ? ? android:background="#545bcb" ? ? ? ? android:text="登錄" ? ? ? ? android:textColor="#ffffff" ? ? ? ? android:textSize="20sp"/> ? ? <Button ? ? ? ? android:id="@+id/login_btn_register" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginLeft="260dp" ? ? ? ? android:layout_marginTop="52dp" ? ? ? ? android:background="#e52525" ? ? ? ? android:text="注冊" ? ? ? ? android:textColor="#ffffff" ? ? ? ? android:textSize="20sp"/> </RelativeLayout> <ImageView ? ? android:layout_width="300dp" ? ? android:layout_height="150dp" ? ? android:id="@+id/logo" ? ? android:layout_alignParentRight="true" ? ? android:layout_alignParentEnd="true" ? ? android:layout_alignParentLeft="true" ? ? android:layout_alignParentStart="true" ? ? android:layout_alignParentTop="true" ? ? android:layout_alignWithParentIfMissing="false" ? ? android:background="#ffffff" ? ? android:src="@drawable/user"/> <EditText ? ? android:layout_width="400dp" ? ? android:layout_height="60dp" ? ? android:inputType="textPassword" ? ? android:ems="10" ? ? android:id="@+id/login_edit_pwd" ? ? android:drawableLeft="@android:drawable/ic_lock_idle_lock" ? ? android:hint="請輸入您的密碼" ? ? android:layout_below="@+id/login_edit_account" ? ? android:layout_alignParentLeft="true" ? ? android:layout_alignParentStart="true" ? ? /> <EditText ? ? android:layout_width="400dp" ? ? android:layout_height="60dp" ? ? android:inputType="textPersonName" ? ? android:id="@+id/login_edit_account" ? ? android:drawableLeft="@android:drawable/ic_menu_myplaces" ? ? android:hint="請輸入您的用戶名" ? ? android:layout_below="@+id/logo" ? ? android:layout_alignParentLeft="true" ? ? android:layout_alignParentStart="true" ? ? android:layout_marginTop="20dp" ? ? /> <LinearLayout ? ? android:orientation="horizontal" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" ? ? android:layout_below="@+id/login_edit_pwd" ? ? > ? ? <CheckBox ? ? ? ? android:id="@+id/Login_Remember" ? ? ? ? android:layout_width="200dp" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? android:checked="false" ? ? ? ? android:text="記住密碼" ? ? ? ? android:textSize="15sp"/> ? ? <Button ? ? ? ? android:id="@+id/login_btn_forgetregister" ? ? ? ? android:layout_width="200dp" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginRight="0dp" ? ? ? ? android:backgroundTint="#ffffff" ? ? ? ? android:text="忘記密碼" ? ? ? ? android:textColor="@color/black" ? ? ? ? android:textSize="15sp"/> </LinearLayout> </RelativeLayout> </RelativeLayout>
MainActivity.java:
package com.example.myapplication;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AlertDialog;
?import androidx.appcompat.app.AppCompatActivity;
?public class MainActivity extends AppCompatActivity{
? ? ?@Override
? ? ?protected void onCreate(Bundle savedInstanceState){
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button button=(Button)findViewById(R.id.login_btn_forgetregister);
? ? ? ? button.setOnClickListener(new View.OnClickListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v){new AlertDialog.Builder(MainActivity.this).setTitle("系統提示").setMessage("請輸入驗證信息進行驗證!")
? ? ? ? ? ? ? ? ? ? .setPositiveButton("確定",new DialogInterface.OnClickListener(){
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog,int which){
? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? }).setNegativeButton("返回",new DialogInterface.OnClickListener(){
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog,int which){
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? }).show();
? ? ? ? }
? ? ? ? });
? ? ? ? Button button1=(Button)findViewById(R.id.login_btn_login);
? ? ? ? button1.setOnClickListener(new View.OnClickListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v){
? ? ? ? ? ? ? ? new AlertDialog.Builder(MainActivity.this).setTitle("系統提示").setMessage("驗證成功!")
? ? ? ? .setNegativeButton("確定",new DialogInterface.OnClickListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(DialogInterface dialog,int which){
? ? ? ? }
? ? ? ? }).show();
? ? ? ? }
? ? ? ? });
? ? ? ? Button button2=(Button)findViewById(R.id.login_btn_register);
? ? ? ? button2.setOnClickListener(new View.OnClickListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(View v){
? ? ? ? new AlertDialog.Builder(MainActivity.this).setTitle("系統提示").setMessage("注冊成功!")
? ? ? ? .setNegativeButton("確定",new DialogInterface.OnClickListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(DialogInterface dialog,int which){
? ? ? ? }
? ? ? ? }).show();
? ? ? ? }
? ? ? ? });
? ?}
}
運行結果
原文鏈接:https://blog.csdn.net/LOVE_105/article/details/112340934
相關推薦
- 2023-07-13 css 為什么要清除浮動
- 2022-05-11 深入理解AQS之Semaphore&CountDownLatch&Cyclic詳解
- 2023-01-11 Android開發中父組件調用子組件方法demo_Android
- 2022-10-11 Filter過濾器和Listener監聽器
- 2023-01-21 C#實現Word轉換RTF的示例代碼_C#教程
- 2022-07-20 python計算機視覺實現全景圖像拼接示例_python
- 2022-10-29 STDC分割網絡:onnx推理
- 2022-04-07 Swift實現簡單計算器項目_Swift
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支