android app登录代码 安卓app登录界面代码_android


今天给大家带来一期使用Kotlin的第二部分,创建首页登录界面吧,之前我有写过Kotlin创建安卓应用程序的第一部分的相关教程和实例,那么今天我就上一篇的基础上进行深入的教程,第二篇也就是第二部分,创建一个登录界面,后面预计还有3个部分,想要学习的伙伴们可要关注我以便接收剩下的几个部分。好了,开始下面的实战吧。

登录界面大概是这样子的


android app登录代码 安卓app登录界面代码_登录界面_02


第一步:创建一个新项目

打开我们的Android Studio,然后选择“创建新项目”


android app登录代码 安卓app登录界面代码_登录界面_03


根据需要命名你的应用程序,不要忘记选中“包括Kotlin支持”,然后单击“下一步”,然后你可以选择运行的应用程序的最低操作系统。


android app登录代码 安卓app登录界面代码_登录界面设计代码_04


之后,单击下一步,然后选择Emply Activily。


android app登录代码 安卓app登录界面代码_登录界面设计代码_05


第二部分:界面设计

(1)制作渐变背景

方法是右键单击可绘制对象,然后选择“ 新建”,然后选择“ 可绘制资源文件”,然后将其命名为bggradient。然后我们将创建一个渐变,就像这样。


<item>
        <shape>
            <gradient
                android:startColor="#1e88e5"
                android:centerColor="#673ab7"
                android:endColor="#4527a0"
                android:angle="45"
                ></gradient>
        </shape>
    </item>


上面的代码将产生这样的渐变。


android app登录代码 安卓app登录界面代码_android_06


如果要使用其他颜色,则只需用所需的颜色替换即可。之后,我们将设计XML布局。在主面板中单击activity_main.xml。这次我们使用布局,在创建渐变之后,现在将渐变添加到activity_main.xml布局中,在android.support.constraint.ConstraintLayout部分中键入此代码


android:background="@drawable/bggradient"


为了进行这样的显示,我们使用了称为Card View的组件。


android app登录代码 安卓app登录界面代码_登录界面_02


<android.support.v7.widget.CardView
            android:layout_width="300dp"
            android:layout_height="50dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:cardCornerRadius="15dp"
            app:cardElevation="20dp"
            android:id="@+id/btnLogin"
            android:clickable="true"
            android:foreground="?android:attr/selectableItemBackground"
            app:cardBackgroundColor="@color/colorAccent"
            android:layout_marginBottom="50dp">
        <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <TextView
                    android:text="Login"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginTop="16dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:id="@+id/textView"
                    android:layout_marginBottom="8dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:fontFamily="sans-serif"
                    android:textColor="@android:color/background_light"
                    android:textSize="24sp"
                    app:layout_constraintVertical_bias="1.0"/>
        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
            android:layout_width="320dp"
            android:layout_height="400dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/btnLogin"
            app:cardCornerRadius="15dp"
            app:cardElevation="20dp"
            app:layout_constraintVertical_bias="0.781"
            android:clickable="true"
            android:foreground="?android:attr/selectableItemBackground"
            android:id="@+id/cardView2">
        <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <ImageView
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    app:srcCompat="@drawable/logo"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:id="@+id/imageView"
                    android:layout_marginTop="36dp"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_marginBottom="8dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintVertical_bias="0.0"/>
            <android.support.design.widget.TextInputLayout
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:id="@+id/textInputLayout2"
                    app:layout_constraintTop_toBottomOf="@+id/imageView"
                    android:layout_marginTop="50dp"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    app:passwordToggleTint="@color/colorAccent">

                <android.support.design.widget.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Email"
                        android:inputType="textEmailAddress" android:id="@+id/inputEmail"/>
            </android.support.design.widget.TextInputLayout>
            <android.support.design.widget.TextInputLayout
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginBottom="16dp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent" android:id="@+id/textInputLayout3"
                    android:layout_marginTop="8dp"
                    app:layout_constraintTop_toBottomOf="@+id/textInputLayout2"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    app:passwordToggleTint="@color/colorAccent">

                <android.support.design.widget.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Password"
                        android:inputType="textPassword" android:id="@+id/inputPassword"/>
            </android.support.design.widget.TextInputLayout>
        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>


通过以下方式将以上代码添加到布局中:


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bggradient"
        tools:context=".MainActivity">

        <!--ISI DI DALAM SINI-->

</android.support.constraint.ConstraintLayout>


那么结果将是这样


android app登录代码 安卓app登录界面代码_登录界面_02


第三部分:科特林时间

如果现在显示完毕,我们将输入最简单的编码部分,你要做的就是单击菜单上java文件夹中的MainActivity,然后按照以下代码并在onCreate方法中键入它。


btnLogin.setOnClickListener {
            val email = inputEmail.text.toString()
            val password = inputPassword.text.toString()
            if (email.isEmpty()|| password.isEmpty()) {
                Toast.makeText(this, "Please Insert Email and Password", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if(email == "admin01@gmail.com" || password == "admin01"){
                val progressDialog = ProgressDialog(this,
                    R.style.Theme_MaterialComponents_Light_Dialog)
                progressDialog.isIndeterminate = true
                progressDialog.setMessage("Loading...")
                progressDialog.show()
                val intent = Intent (this,Dashboard::class.java)
                startActivity(intent)
                finish()
            }
        }


单词的Dashboard部分将为红色/错误,但不要惊慌,因为我们将登录activity定向到Dashboard activity,但尚未创建Dashboard activity,因此,我们现在要做的就是创建Dashboard activity。很简单,单击文件 > 活动 > 空活动,然后将activity命名为仪表板。


android app登录代码 安卓app登录界面代码_xml_09


第四部分:测试应用

要从Android Studio运行程序,我们需要有一个模拟器或使用一台Android设备。


android app登录代码 安卓app登录界面代码_登录界面_02