用Android Studio编写一个登录界面和注册界面并可以跳转 ...

打印 上一主题 下一主题

主题 536|帖子 536|积分 1608

下面是利用 Android Studio 编写一个简单的登录界面和注册界面,并实现跳转的示例代码。
起首,在 res/layout 目录下创建一个名为 activity_login.xml 的结构文件,作为登录界面的结构:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     android:orientation="vertical">
  5.     <EditText
  6.         android:id="@+id/editTextEmail"
  7.         android:layout_width="match_parent"
  8.         android:layout_height="wrap_content"
  9.         android:hint="Email" />
  10.     <EditText
  11.         android:id="@+id/editTextPassword"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="wrap_content"
  14.         android:hint="Password"
  15.         android:inputType="textPassword" />
  16.     <Button
  17.         android:id="@+id/buttonLogin"
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"
  20.         android:text="Login" />
  21.     <TextView
  22.         android:id="@+id/textViewRegister"
  23.         android:layout_width="wrap_content"
  24.         android:layout_height="wrap_content"
  25.         android:text="Register" />
  26. </LinearLayout>
复制代码

接下来,在 res/layout 目录下创建一个名为 activity_register.xml 的结构文件,作为注册界面的结构:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     android:orientation="vertical">
  5.     <EditText
  6.         android:id="@+id/editTextName"
  7.         android:layout_width="match_parent"
  8.         android:layout_height="wrap_content"
  9.         android:hint="Name" />
  10.     <EditText
  11.         android:id="@+id/editTextEmail"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="wrap_content"
  14.         android:hint="Email" />
  15.     <EditText
  16.         android:id="@+id/editTextPassword"
  17.         android:layout_width="match_parent"
  18.         android:layout_height="wrap_content"
  19.         android:hint="Password"
  20.         android:inputType="textPassword" />
  21.     <Button
  22.         android:id="@+id/buttonRegister"
  23.         android:layout_width="wrap_content"
  24.         android:layout_height="wrap_content"
  25.         android:text="Register" />
  26. </LinearLayout>
复制代码

然后,在 LoginActivity.java 中编写登录界面的逻辑代码:
  1. import android.content.Intent;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.TextView;
  7. public class LoginActivity extends AppCompatActivity {
  8.    
  9.     private TextView textViewRegister;
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_login);
  14.         textViewRegister = findViewById(R.id.textViewRegister);
  15.         textViewRegister.setOnClickListener(new View.OnClickListener() {
  16.             @Override
  17.             public void onClick(View v) {
  18.                 Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  19.                 startActivity(intent);
  20.             }
  21.         });
  22.     }
  23. }
复制代码

末了,在 RegisterActivity.java 中编写注册界面的逻辑代码:
  1. import android.support.v7.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. public class RegisterActivity extends AppCompatActivity {
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_register);
  8.     }
  9. }
复制代码

以上代码就是一个简单的登录界面和注册界面的实现,并且可以通过点击 "Register" 文本举行跳转。记得在 AndroidManifest.xml 文件中注册 LoginActivity 和 RegisterActivity。如许,您就可以在 Android Studio 中运行应用程序,并测试登录界面和注册界面的跳转功能了。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表