赞
踩
下面是使用 Android Studio 编写一个简单的登录界面和注册界面,并实现跳转的示例代码。
首先,在 res/layout 目录下创建一个名为 activity_login.xml 的布局文件,作为登录界面的布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <EditText
- android:id="@+id/editTextEmail"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Email" />
-
- <EditText
- android:id="@+id/editTextPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password"
- android:inputType="textPassword" />
-
- <Button
- android:id="@+id/buttonLogin"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Login" />
-
- <TextView
- android:id="@+id/textViewRegister"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Register" />
- </LinearLayout>
接下来,在 res/layout 目录下创建一个名为 activity_register.xml 的布局文件,作为注册界面的布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <EditText
- android:id="@+id/editTextName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Name" />
-
- <EditText
- android:id="@+id/editTextEmail"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Email" />
-
- <EditText
- android:id="@+id/editTextPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Password"
- android:inputType="textPassword" />
-
- <Button
- android:id="@+id/buttonRegister"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Register" />
- </LinearLayout>
然后,在 LoginActivity.java 中编写登录界面的逻辑代码:
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class LoginActivity extends AppCompatActivity {
-
- private TextView textViewRegister;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
-
- textViewRegister = findViewById(R.id.textViewRegister);
- textViewRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
- startActivity(intent);
- }
- });
- }
- }
最后,在 RegisterActivity.java 中编写注册界面的逻辑代码:
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
-
- public class RegisterActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- }
- }
以上代码就是一个简单的登录界面和注册界面的实现,并且可以通过点击 "Register" 文本进行跳转。记得在 AndroidManifest.xml 文件中注册 LoginActivity 和 RegisterActivity。这样,您就可以在 Android Studio 中运行应用程序,并测试登录界面和注册界面的跳转功能了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。