一、新建一個空的LoginActivity
二、在activity.login.xml中進行佈局設計 (一)那麼如何打開呢這個.xml文件呢? 新建activity後裡面會有初始代碼,按住Ctrl鍵點擊activity.login就可以進入xml文件瞭。
(二)如何進行登錄界面的佈局設計呢? 首先把這個xml改成線性佈局,把這個改成LinearLayout。
然後進行佈局界面設計
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/white" android:orientation="vertical" tools:context=".LoginActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/purple_200" android:textSize="40dp" android:background="@color/white" android:gravity="center" android:text="登錄系統"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:textSize="25sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:text="用戶名"/> </LinearLayout> <EditText android:id="@+id/login_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" > </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密碼" android:textColor="@color/black" android:textSize="25sp" /> </LinearLayout> <EditText android:id="@+id/login_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword"> </EditText> <LinearLayout android:layout_marginTop="15dp" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/login_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="登錄"> </Button> <Button android:id="@+id/reg_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="註冊"> </Button> </LinearLayout></LinearLayout>
佈局界面展示
三、點擊進行頁面進行跳轉 (一)新建一個註冊activity與主頁面activity 首先呢,咱們得先有個跳轉後的主頁面和註冊頁面,這裡為瞭方便演示,就簡單整個註冊頁面和主頁面吧!狗頭保命
(二)分別在這倆佈局裡面隨便寫點標識的東西
(三)在LoginActivity中用Intent對其實現跳轉
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Button button = findViewById(R.id.login_btn); Button button1 = findViewById(R.id.reg_btn); EditText e_username = findViewById(R.id.login_name); EditText e_password = findViewById(R.id.login_password); String username = e_username.getText().toString(); String password = e_password.getText().toString(); //點擊登錄按鈕,實現跳轉 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(LoginActivity.this,username,Toast.LENGTH_SHORT); Toast.makeText(LoginActivity.this,password,Toast.LENGTH_SHORT); Intent intent = new Intent(LoginActivity.this,HomeActivity.class); startActivity(intent); } }); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(LoginActivity.this,RestActivity.class); startActivity(intent); } }); }}
狗頭三連 ~~~~~~~~~~~~~~~~~~~