登錄界面(mainActivity.xml)
<?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:id="@+id/wjh" 設(shè)置id,給mainActivity.java文件調(diào)用。
android:background="@drawable/picture"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="143dp"
android:text="@string/login"
android:textSize="100sp"
android:textColor="@color/black"
android:gravity="center">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="570dp"
android:orientation="vertical"> 設(shè)置布局豎直排列
<Space
android:layout_width="match_parent"
android:layout_height="100dp" />
<EditText
android:id="@+id/editTextText2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="@string/username"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_gravity="center"
android:gravity="center"
/>
<Space
android:layout_width="match_parent"
android:layout_height="100dp" />
<EditText
android:id="@+id/editTextTextPassword6"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:ems="10"
android:textStyle="bold"
android:inputType="textPassword"
android:hint="@string/password"
android:gravity="center"
android:layout_gravity="center"
android:textSize="20sp"
/>
<Space
android:layout_width="match_parent"
android:layout_height="100dp" />
<Button
android:id="@+id/button"
android:layout_width="150dp"
android:layout_height="83dp"
android:layout_gravity="center"
android:text="@string/button"
android:onClick="Jump"
android:textColor="@color/cardview_light_background"
android:background="@drawable/p2"
/>
</LinearLayout>
</LinearLayout>
效果圖
?文章來源地址http://www.zghlxwxcb.cn/news/detail-856651.html
跳轉(zhuǎn)界面(activity_main2.xml)
<?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">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/success"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="100dp"
android:text="@string/success"
android:textColor="@color/black"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Space
android:layout_width="wrap_content"
android:layout_height="200dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="199dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
如何將兩個(gè)activity結(jié)合?
編輯mainActivity2.java文件
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
onCreate() 方法是 Android 活動(dòng)(Activity)的生命周期方法之一,它在活動(dòng)被創(chuàng)建時(shí)調(diào)用。其主要作用是初始化活動(dòng)的用戶界面,設(shè)置布局內(nèi)容和相關(guān)資源,并執(zhí)行一些必要的初始化操作。
在 onCreate() 方法中,通常會(huì)執(zhí)行以下操作:
1.設(shè)置布局:調(diào)用 setContentView() 方法設(shè)置活動(dòng)的布局文件,即指定要在屏幕上顯示的用戶界面。
2.獲取 Intent 數(shù)據(jù):如果該活動(dòng)需要從其他活動(dòng)接收數(shù)據(jù),通常會(huì)通過 getIntent() 方法獲取傳遞過來的 Intent 對(duì)象,并從中提取所需的數(shù)據(jù)。
3.初始化界面元素:通過調(diào)用 findViewById() 方法初始化界面中的各種 UI 元素,例如按鈕、文本框、圖像視圖等。
4.設(shè)置事件監(jiān)聽器:為界面元素設(shè)置事件監(jiān)聽器,以便在用戶與之交互時(shí)執(zhí)行相應(yīng)的操作。
5.執(zhí)行其他初始化操作:執(zhí)行其他必要的初始化操作,例如數(shù)據(jù)加載、網(wǎng)絡(luò)請(qǐng)求、權(quán)限檢查等
public class mainActivity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
引入mainActivity2.xml,使得兩個(gè)文件相互關(guān)聯(lián)。
Intent intent = getIntent();
intent用來存放數(shù)據(jù),進(jìn)行對(duì)數(shù)據(jù)的操作,
在 Android 中,Intent 是用于在不同組件之間傳遞數(shù)據(jù)的對(duì)象
String userInput = intent.getStringExtra("userInput");
取到與自己通信的activity傳遞來的數(shù)據(jù),intent內(nèi)部存儲(chǔ)數(shù)據(jù)
采用的是key-value結(jié)構(gòu)。此時(shí)的key是userInput
TextView textView = findViewById(R.id.textView);
與自己的activity.xml的文本框綁定
textView.setText(userInput);
將文本設(shè)置進(jìn)文本框
}
}
AndroidManifest.xml中注冊(cè)新加的activity
</activity>
<activity android:name=".mainActivity2"/>
activity_main.xml?
<Button
android:id="@+id/button"
android:layout_width="150dp"
android:layout_height="83dp"
android:layout_gravity="center"
android:text="@string/button"
android:onClick="Jump" 注冊(cè)Jump方法,在java文件中定義實(shí)現(xiàn)
android:textColor="@color/cardview_light_background"
android:background="@drawable/p2"
/>
mainActivity.java (實(shí)現(xiàn)點(diǎn)擊按鈕的方法)
public void Jump(View view){
EditText editText = findViewById(R.id.editTextText2);
String userInput = editText.getText().toString();
取到main_activity.xml中的輸入的用戶名一欄的數(shù)據(jù)
Intent intent = new Intent(this, mainActivity2.class);
創(chuàng)建intent對(duì)象進(jìn)行數(shù)據(jù)交互
intent.putExtra("userInput", userInput);
發(fā)送數(shù)據(jù),設(shè)置key值為userInput,value為取到的數(shù)據(jù)
startActivity(intent);
執(zhí)行
}
文章來源:http://www.zghlxwxcb.cn/news/detail-856651.html
到了這里,關(guān)于Android開發(fā)——實(shí)現(xiàn)簡易登錄功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!