AndroidStudio创建的LoginActivity分析

从今天起记录一下对Android一些知识的学习吧。

感觉AS中生成的LoginActivity布局结构很简洁,稍加修改就能有很好的效果,所以学习一下这个Activity的代码。

首先是布局文件

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dc.activity.LoginActivity">

    <!-- Login progress -->
    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone"/>

    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/email_login_form"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation=
  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Studio 中写登录页面需要以下步骤: 1. 创建一个新的 Android 项目,并在其中创建一个新的 Activity 作为登录页面。在 res/layout 文件夹中创建一个名为 activity_login.xml 的布局文件,用于显示登录界面的 UI。 2. 在 activity_login.xml 中添加用户名和密码的 EditText 组件,并添加一个登录按钮。 3. 在 Login Activity 的 Java 代码中,使用 findViewById() 方法获取用户名和密码的 EditText 组件,并设置登录按钮的 OnClickListener() 方法,以便在单击登录按钮时执行登录操作。 4. 在登录操作中,您可以使用 SharedPreferences 或 SQLite 数据库来存储用户的登录信息。 5. 如果用户成功登录,则可以启动应用程序的主 Activity,并关闭登录 Activity。否则,您可以显示一个错误消息,提示用户输入正确的用户名和密码。 以下是一个简单的示例代码: activity_login.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:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login" /> </LinearLayout> ``` LoginActivity.java: ``` public class LoginActivity extends AppCompatActivity { private EditText mUsernameEditText; private EditText mPasswordEditText; private Button mLoginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mUsernameEditText = findViewById(R.id.username); mPasswordEditText = findViewById(R.id.password); mLoginButton = findViewById(R.id.login_button); mLoginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = mUsernameEditText.getText().toString(); String password = mPasswordEditText.getText().toString(); if (isValidCredentials(username, password)) { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_SHORT).show(); } } }); } private boolean isValidCredentials(String username, String password) { // TODO: Implement your authentication logic here return true; } } ``` 在 isValidCredentials() 方法中,您可以添加您的身份验证逻辑,例如检查用户名和密码是否与存储在 SharedPreferences 或 SQLite 数据库中的值匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值