您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Android怎么實現簡易登陸注冊”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Android怎么實現簡易登陸注冊”這篇文章吧。
activity_login
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/tv_username"android:layout_width="wrap_content"android:layout_height="40dp"android:layout_marginStart="40dp"android:layout_marginTop="100dp"android:gravity="center_vertical"android:text="賬號:"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_password"android:layout_width="wrap_content"android:layout_height="40dp"android:layout_marginStart="40dp"android:gravity="center_vertical"android:text="密碼:"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/tv_username" /><EditTextandroid:id="@+id/ed_username"android:layout_width="300dp"android:layout_height="40dp"android:layout_marginTop="100dp"app:layout_constraintLeft_toRightOf="@id/tv_username"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:id="@+id/ed_password"android:layout_width="300dp"android:inputType="textPassword"android:layout_height="40dp"app:layout_constraintLeft_toRightOf="@id/tv_password"app:layout_constraintTop_toBottomOf="@id/ed_username" /><Buttonandroid:id="@+id/bu_login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="300dp"android:text="登錄"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@id/bu_register"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/bu_register"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="300dp"android:text="注冊"app:layout_constraintLeft_toRightOf="@id/bu_login"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
LoginActivity
package com.jld.exam;import android.content.Intent;import android.os.Bundle;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class LoginActivity extends AppCompatActivity {EditText ed_username;EditText ed_password;Button bu_login;Button bu_register;String username;String password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);ed_username = findViewById(R.id.ed_username);ed_password = findViewById(R.id.ed_password);bu_login = findViewById(R.id.bu_login);bu_register = findViewById(R.id.bu_register);//登錄按鈕監聽bu_login.setOnClickListener(v -> {username = ed_username.getText().toString();password = ed_password.getText().toString();//登陸的簡單邏輯if (username.equals("")) {Toast.makeText(LoginActivity.this, "請輸入用戶名", Toast.LENGTH_SHORT).show();} else if (password.equals("")) {Toast.makeText(LoginActivity.this, "請輸入密碼", Toast.LENGTH_SHORT).show();} else if (!username.equals("root") || !password.equals("123456")) {Toast.makeText(LoginActivity.this, "用戶名或密碼錯誤", Toast.LENGTH_SHORT).show();} else {Intent intent = new Intent(LoginActivity.this, MainActivity.class);startActivity(intent);}});//注冊按鈕監聽bu_register.setOnClickListener(v -> {Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);startActivity(intent);});}}
activity_main
<?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:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="商品列表"android:layout_marginBottom="30dp"android:gravity="center_horizontal"/><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recycler"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
MainActivity
package com.jld.exam;import android.graphics.Color;import android.os.Bundle;import android.widget.LinearLayout;import androidx.appcompat.app.AppCompatActivity;import androidx.recyclerview.widget.GridLayoutManager;import androidx.recyclerview.widget.RecyclerView;public class MainActivity extends AppCompatActivity {RecyclerViewAdapter recyclerViewAdapter;private final int[] icno = {R.drawable.clock, R.drawable.signal, R.drawable.box,R.drawable.second, R.drawable.elephone, R.drawable.ff, R.drawable.notebook, R.drawable.mark, R.drawable.yx,R.drawable.shop, R.drawable.theme, R.drawable.xl,};private final String[] name = {"時鐘", "信號", "寶箱", "秒鐘", "大象", "FF", "記事本", "書簽", "印象", "商店", "主題", "迅雷"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//GridLayoutManagerGridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 3);//創建recyclerviewRecyclerView recyclerView = findViewById(R.id.recycler);//設定布局管理器recyclerView.setLayoutManager(gridLayoutManager);//創建適配器recyclerViewAdapter = new RecyclerViewAdapter(icno, name);recyclerView.setAdapter(recyclerViewAdapter);//設定適配器}}
activity_register
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".RegisterActivity"><TextViewandroid:id="@+id/textView0"android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center"android:text="用戶注冊"android:textSize="30sp"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textView1"android:layout_width="100dp"android:layout_height="40dp"android:drawableStart="@drawable/account"android:gravity="center_vertical"android:text="用戶名:"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/textView0" /><EditTextandroid:id="@+id/editText1"android:layout_width="300dp"android:layout_height="40dp"android:layout_weight="10"android:autofillHints=""android:gravity="center_horizontal"android:inputType="text"app:layout_constraintLeft_toRightOf="@id/textView1"app:layout_constraintTop_toBottomOf="@id/textView0" /><TextViewandroid:id="@+id/textView2"android:layout_width="100dp"android:layout_height="40dp"android:layout_weight="2"android:drawableStart="@drawable/password"android:gravity="center_vertical"android:text="新密碼:"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/textView1" /><EditTextandroid:id="@+id/editText2"android:layout_width="300dp"android:layout_height="40dp"android:layout_weight="10"android:autofillHints=""android:gravity="center_horizontal"android:inputType="textPassword"app:layout_constraintLeft_toRightOf="@id/textView2"app:layout_constraintTop_toBottomOf="@id/editText1" /><TextViewandroid:id="@+id/textView5"android:layout_width="100dp"android:layout_height="40dp"android:drawableStart="@drawable/phone"android:gravity="center_vertical"android:text="手機電話:"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/textView2" /><EditTextandroid:id="@+id/editText5"android:layout_width="300dp"android:layout_height="40dp"android:layout_weight="10"android:autofillHints=""android:gravity="center_horizontal"android:inputType="phone"app:layout_constraintLeft_toRightOf="@id/textView5"app:layout_constraintTop_toBottomOf="@id/editText2" /><TextViewandroid:id="@+id/textView7"android:layout_width="100dp"android:layout_height="40dp"android:drawableStart="@drawable/email"android:gravity="center_vertical"android:text="E_mail:"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/editText5" /><EditTextandroid:id="@+id/editText7"android:layout_width="300dp"android:layout_height="40dp"android:layout_weight="10"android:autofillHints=""android:gravity="center_horizontal"android:inputType="textEmailAddress"app:layout_constraintLeft_toRightOf="@id/textView7"app:layout_constraintTop_toBottomOf="@id/editText5" /><TextViewandroid:id="@+id/textView8"android:layout_width="100dp"android:layout_height="40dp"android:drawableStart="@drawable/gender"android:gravity="center_vertical"android:text="性別:"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@id/textView7" /><RadioGroupandroid:id="@+id/radioGroup8"android:layout_width="300dp"android:layout_height="40dp"android:layout_weight="10"android:gravity="center_vertical"android:orientation="horizontal"app:layout_constraintLeft_toRightOf="@id/textView8"app:layout_constraintTop_toBottomOf="@id/editText7"><RadioButtonandroid:id="@+id/radioButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女" /><RadioButtonandroid:id="@+id/radioButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男" /></RadioGroup><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="40dp"android:text="保存"app:layout_constraintEnd_toStartOf="@id/button2"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@id/radioGroup8" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="40dp"android:text="取消"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toEndOf="@id/button1"app:layout_constraintTop_toBottomOf="@id/radioGroup8" /></androidx.constraintlayout.widget.ConstraintLayout>
RegisterActivity
package com.jld.exam;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class RegisterActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);}}
recyc_item
<?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="wrap_content"android:orientation="vertical"><ImageViewandroid:id="@+id/iv_image"android:layout_width="60dp"android:layout_height="60dp"android:layout_gravity="center_horizontal"android:contentDescription="TODO"android:scaleType="centerInside"android:src="@drawable/ic_launcher_background" /><TextViewandroid:id="@+id/tv_desc"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:hint="銷售價格"android:textSize="14sp" /></LinearLayout>
RecyclerViewAdapter
package com.jld.exam;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import androidx.annotation.NonNull;import androidx.recyclerview.widget.RecyclerView;public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {private final int[] icno;private final String[] desc;public RecyclerViewAdapter(int[] icno, String[] desc) {this.icno = icno;this.desc = desc;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyc_item, parent, false);return new ViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {holder.imageView.setImageResource(icno[position]);holder.textView.setText(desc[position]);}@Overridepublic int getItemCount() {return icno.length;}//ViewHolderpublic static class ViewHolder extends RecyclerView.ViewHolder {View item;ImageView imageView;TextView textView;public ViewHolder(@NonNull View itemView) {super(itemView);item = itemView;imageView = itemView.findViewById(R.id.iv_image);textView = itemView.findViewById(R.id.tv_desc);}}}
以上是“Android怎么實現簡易登陸注冊”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。