安卓實現登錄與注冊界面

使用Intent與Bundle傳遞數據

登錄界面login.xml

在這里插入圖片描述

1.使用Relativelayout相對布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="20dp"android:paddingTop="20dp"><TextViewandroid:id="@+id/tv_username"style="@style/intent_login_textview"android:text="@string/username"/><EditTextandroid:id="@+id/et_username"style="@style/intent_login_edittext"android:layout_toRightOf="@id/tv_username"android:hint="填寫用戶名" /><TextViewandroid:id="@+id/tv_password"style="@style/intent_login_textview"android:layout_below="@id/tv_username"android:text="@string/password"android:layout_marginTop="20dp"/><EditTextandroid:id="@+id/et_passsword"style="@style/intent_login_edittext"android:layout_below="@id/et_username"android:layout_toRightOf="@id/tv_password"android:hint="填寫密碼" /><Buttonandroid:id="@+id/btn_register"android:layout_marginTop="100dp"style="@style/intent_login_button"android:layout_marginLeft="40dp"android:text="注冊"/><Buttonandroid:id="@+id/btn_login"android:layout_marginTop="100dp"android:layout_marginLeft="200dp"style="@style/intent_login_button"android:text="登錄"/>
</RelativeLayout>

2.樣式,themes.xml中

 <style name="intent_login_textview"><item name="android:layout_width">50dp</item><item name="android:layout_height">wrap_content</item><item name="textAllCaps">false</item><item name="android:textColor">@color/black</item><item name="android:textSize">15sp</item><item name="android:textStyle">normal</item></style><style name="intent_login_edittext"><item name="android:layout_width">match_parent</item><item name="android:layout_height">wrap_content</item><item name="textAllCaps">false</item><item name="android:textColor">@color/black</item><item name="android:textSize">10sp</item><item name="android:textStyle">normal</item></style><style name="intent_login_button"><item name="android:layout_width">wrap_content</item><item name="android:layout_height">wrap_content</item><item name="textAllCaps">false</item><item name="android:textColor">@color/black</item><item name="android:textSize">20sp</item><item name="android:textStyle">normal</item></style>

LoginActivity.java

public class LoginActivity extends AppCompatActivity {private EditText et_password, et_username;private Button btn_register, btn_login;private String dq_name, dq_password;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);//設置Activity顯示的布局et_password = findViewById(R.id.et_passsword);//獲取布局中的控件對象et_username = findViewById(R.id.et_username);btn_login = findViewById(R.id.btn_login);btn_register = findViewById(R.id.btn_register);Btnlitener btnlistener = new Btnlitener();btn_login.setOnClickListener(btnlistener);btn_register.setOnClickListener(btnlistener);}private class Btnlitener implements View.OnClickListener {@Overridepublic void onClick(View v) {Intent intent;switch (v.getId()) {case R.id.btn_register:String username = et_username.getText().toString();String password = et_password.getText().toString();intent = new Intent(LoginActivity.this, RegisterActivity.class);intent.putExtra("username", username);intent.putExtra("password", password);startActivity(intent);break;case R.id.btn_login:String username1 = et_username.getText().toString();String password1 = et_password.getText().toString();intent = getIntent();Bundle bundle = intent.getExtras();dq_name = bundle.getString("dq_name");dq_password = bundle.getString("dq_password");if (username1.equals("")||password1.equals("")){Toast.makeText(LoginActivity.this, "用戶名或密碼不能為空", Toast.LENGTH_SHORT).show();}else {if (username1.equals(dq_name) && (password1.equals(dq_password))) {startActivity(new Intent(LoginActivity.this, NextActivity.class));} else {Toast.makeText(LoginActivity.this, "用戶名或密碼錯誤", Toast.LENGTH_SHORT).show();}}break;}}}}

register注冊.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="20dp"android:paddingTop="20dp"><TextViewandroid:id="@+id/tv_username"style="@style/intent_login_textview"android:text="@string/username"/><EditTextandroid:id="@+id/et_username"style="@style/intent_login_edittext"android:layout_toRightOf="@id/tv_username"android:hint="填寫用戶名" /><TextViewandroid:id="@+id/tv_password"style="@style/intent_login_textview"android:layout_below="@id/tv_username"android:text="@string/password"android:layout_marginTop="20dp"/><EditTextandroid:id="@+id/et_passsword"style="@style/intent_login_edittext"android:layout_below="@id/et_username"android:layout_toRightOf="@id/tv_password"android:hint="填寫密碼" /><TextViewandroid:id="@+id/tv_repassword"style="@style/intent_login_textview"android:layout_below="@id/tv_password"android:text="@string/repassword"android:layout_marginTop="20dp"/><EditTextandroid:id="@+id/et_repasssword"style="@style/intent_login_edittext"android:layout_below="@id/et_passsword"android:layout_toRightOf="@id/tv_repassword"android:hint="再次填寫密碼" /><Buttonandroid:id="@+id/btn_confirm"android:layout_marginTop="150dp"style="@style/intent_login_button"android:layout_marginLeft="40dp"android:text="確認"/><Buttonandroid:id="@+id/btn_cancel"android:layout_marginTop="150dp"android:layout_marginLeft="200dp"style="@style/intent_login_button"android:text="取消"/>
</RelativeLayout>

注冊界面.java

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {private EditText et_username, et_password, et_repassword;private Button btn_cancel, btn_confirm;private int count;private long t;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);et_username = findViewById(R.id.et_username);et_password = findViewById(R.id.et_passsword);et_repassword = findViewById(R.id.et_repasssword);btn_cancel = findViewById(R.id.btn_cancel);btn_confirm = findViewById(R.id.btn_confirm);btn_cancel.setOnClickListener(this::onClick);btn_confirm.setOnClickListener(this::onClick);Intent intent = getIntent();String username = intent.getStringExtra("username");String password = intent.getStringExtra("password");et_username.setText(username);et_password.setText(password);et_repassword.setText(password);t = System.currentTimeMillis();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_cancel:et_repassword.setText("");et_password.setText("");et_username.setText("");long t1 = System.currentTimeMillis();if (t1 - t < 2000) {startActivity(new Intent(RegisterActivity.this, LoginActivity.class));}else {t = System.currentTimeMillis();Toast.makeText(RegisterActivity.this, "連續按兩次回到登錄界面", Toast.LENGTH_LONG).show();}break;case R.id.btn_confirm:if ((!"".equals(et_username.getText().toString())) && (!"".equals(et_password.getText().toString())) && (!"".equals(et_repassword.getText().toString()))) {if (et_password.getText().toString().equals(et_repassword.getText().toString())) {Toast.makeText(RegisterActivity.this, "注冊成功", Toast.LENGTH_LONG).show();Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);String dq_name = et_username.getText().toString();String dq_password = et_password.getText().toString();Bundle bundle = new Bundle();bundle.putString("dq_name", dq_name);bundle.putString("dq_password", dq_password);intent.putExtras(bundle);startActivity(intent);} else {Toast.makeText(RegisterActivity.this, "兩次密碼不一致", Toast.LENGTH_LONG).show();}} else {Toast.makeText(RegisterActivity.this, "不能為空", Toast.LENGTH_LONG).show();}break;}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446792.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446792.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446792.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android Button字母自動全部大寫的問題

兩種解決方案&#xff1a; 方法一&#xff1a; 在 xml 布局中設置屬性 android:textAllCaps"false" <Buttonandroid:layout_width"wrap_content"android:layout_height"match_parent"android:text"添加動作組"android:textAllCap…

安卓Activity與intent跳轉

Activity生命周期 Activity啟動模式 Intent跳轉 _________startActivity() 1.Intent intentnew Intent(A.this,B.class); startActivity(intent); 2.startActivity(new Intent(A.this,B.class)); _________startActivityForResult() Intent intentnew Intent(A.this,B.class…

將讀寫鎖放到共享內存中,實現進程之間對數據的讀寫訪問控制

代碼 #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <assert.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <fstream> #include <…

Android WebView 使用漏洞

目錄一、類型二、具體分析2.1、WebView任意代碼執行漏洞2.1.1、addJavascriptInterface 接口引起遠程代碼執行漏洞漏洞產生原因解決方案關于該方法的其他細節總結2.1.2、searchBoxJavaBridge_接口引起遠程代碼執行漏洞漏洞產生原因解決方案2.1.3、accessibility和 accessibilit…

將讀寫鎖放到共享內存,實現進程之間對于同一文件的讀寫操作

思路 將讀寫鎖和讀寫鎖的屬性以及一個用于存儲共享內存的地址的int型變量三者封裝成一個struct結構將這個結構體放到共享內存中&#xff0c;以及將讀寫鎖的屬性設置成全局性質&#xff0c;然后使用這個屬性初始化鎖&#xff0c;以及將鎖的地址關聯到結構體的內存地址這個變量定…

Android Studio 查看頁面布局層次結構

Android Studio有個可以查看手機上app頁面布局層次結構的工具。可以協助我們對布局進行優化&#xff0c;去掉沒有必要的節點等&#xff0c;通過這個工具可以清晰的看見頁面整個結構&#xff1b;廢話少說直接上圖&#xff0c;再說過程。 這就是我們想要看到的&#xff0c;每個節…

Java web后端 第一章框架搭建

Redis 通用Mapper 通用Mapper->MyBatis動態SQL封裝包,增刪改查 0 SQL語句 PageHelper PageHelper–>實現分頁操作,不需要limit,直接使用靜態方法 電商系統技術特點 分布式(數據很多,一臺電腦存儲一部分數據) 高并發,集群(并發量很高,后臺不只一個電腦) ,海量數據 主…

進程鎖 讀寫文件的小例子 C++代碼

代碼 #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <cassert> #include <pthread.h> #include <cstdio> #include <cstdlib> #include <fstream> #include <io…

Java 中sleep()與wait()的區別

目錄一、原理不同二、鎖的處理機制不同三、使用的區域不同四、異常捕獲不同五、總結一、原理不同 sleep()是Thread類的靜態方法&#xff0c;是線程用來控制自身流程的&#xff0c;它會使此線程暫停執行指定的時間&#xff0c;而把執行機會讓給其他的線程&#xff0c;等到計時時…

android--地圖定位打卡

獲取位置信息 1)位置信息 GPS衛星定位,在室外適用 基站(3個基站交叉,鎖定手機位置)–基站定位不平均,有些地方實現不了3點定位 網絡定位–通過手機IP地址,去鎖定位置(消耗流量,對網絡有要求) 谷歌地圖的大致實現思路(通用) 2)實現定位功能的重要類 在百度地圖和高德地圖中不…

Android 將整形顏色值轉換成String類型

轉換方法&#xff1a; val hexColor String.format("#%06X", [0xFFFFFF or intColor]);轉換結果&#xff1a; #F2EADA

MacOS 的 zsh 和 bash 切換

目錄一、從 bash 切換到 zsh1、使用系統自帶的 zsh2、使用第三方的 zsh2.1、Clone代碼到本地2.2、備份你已存在的 ~/.zshrc 文件2.3、新建一份新的 zsh 配置文件2.4、改變默認的shell腳本二、從 zsh 切換回 bash三、zsh 和 bash 的環境變量zsh、bash 都是shell&#xff0c;zsh …

android--在命令行中生成Android的數字證書keystore文件

標題 生成 密鑰口令為 13458977480 密鑰庫口令為 13458977480 存放位置 查看證書的相關資料

linux查看系統日志

cd /var/log/gscubuntu:/var/log$ tail -f syslog

IDEA 創建 SpringBoot 項目

目錄一、新建Springboot項目第一步&#xff1a;新建一個Springboot項目第二步&#xff1a;選擇項目模板第三步&#xff1a;設置項目配置第四步&#xff1a;設置項目依賴第五步&#xff1a;設置項目名稱及路徑第六步&#xff1a;創建完成二、測試及運行1、測試代碼2、設置默認端…

VC++軟件

一個main fatal error LNK1169: 找到一個或多個多重定義的符號–報錯 一個項目即一個程序&#xff0c;多個文件只能有一個main函數 刪除掉多余的main 控制臺按enter鍵閃退 在代碼中加上 #include<stdlib.h> getchar();//讓控制臺停留 system("pause");//讓…

IDEA 將 SpringBoot 項目打包成jar

目錄一、打包配置1、File -> Project Structure2、Project Structure3、設置啟動類及META-INF4、設置打包輸出目錄二、打包1、Build -> Artifacts2、Build三、查看打包文件四、運行新建SpringBoot項目&#xff1a;IDEA 創建 SpringBoot 項目 一、打包配置 1、File -> …

2014年考研英語一完型填空知識點

單詞 單詞釋意commitv犯罪sufficientlyadv足夠gainfuladj有收益的socioeconomicadj社會經濟的discontentn/v不滿意householdn家庭supervisionn監督offensiveadj冒犯的conditionn狀態casualadj隨意的causaladj因果關系的establishedadj已確立,公認的interactionn相互作用或影響…

如何查看軟連接,以及相關注意事項

使用命令 ls -il 圖片顯示 參考鏈接 Linux 命令之軟連接詳解Linux軟連接 查看/創建/刪除