其他分享
首页 > 其他分享> > 菜鸡的安卓教程:登陆界面

菜鸡的安卓教程:登陆界面

作者:互联网

登陆界面

写登录界面之前我们要知道登陆界面都有什么,首先要有用户名和密码,还要有一个确认登陆的按钮;

所以,第一步先把大体的框架写出来:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"//组件宽度匹配父类
        android:layout_height="wrap_content"//组件高度根据输入的内容来确定
        android:layout_marginTop="25dp"//组件距离界面的上方25dp
        android:layout_marginLeft="15dp"//组件距离界面的左边15dp
        android:layout_marginRight="15dp"//组件距离界面的右边15dp
        android:hint="用户名"  //组件上添加隐藏的提示,就是输入后会自动消失的
        android:paddingLeft="15dp"/>//“用户名”距离组件的左边15dp,不设置的话用户名是紧贴组件左边的

    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:hint="密码"
        android:paddingLeft="15dp"
        android:inputType="textPassword"/>//将输入的密码设置为暗文,就是你输入你的密码,在界面上显示的是一个个点。

    <Button
        android:id="@+id/bu1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:text="确认" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"> //按下按钮之后的效果
        <shape>
            <solid android:color="#9A2EFE"/>//被按下后按钮的填充颜色(紫色)
            <corners android:radius="10dp"/>//按钮框的圆角
        </shape>
    </item>
    <item android:state_pressed="false"> //没有按按钮的效果
        <shape>
            <solid android:color="#00FF80"/>//没有被按的填充颜色(绿色)可以随便设置,百度搜一个取色器
            <corners android:radius="10dp"/>//圆角
        </shape>
    </item>
</selector>

//要想调用,就在按钮组件里加一个background就行
/*
    <Button
        android:id="@+id/bu1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:text="确认"
        android:background="@drawable/pres"/>
*/

以上就是界面的代码;
接下来可以设置用户名和对应的密码了;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".Main2Activity">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="欢迎来到德莱联盟"/>

</LinearLayout>

写控制代码:

  1. 先声明三个组件
    private EditText et1;
    private EditText et2;
    private Button button;
  1. 通过id找到这三个组件
        button = findViewById(R.id.bu1);
        et1 = findViewById(R.id.et1);
        et2 = findViewById(R.id.et2);
  1. 给按钮添加监听
button.setOnClickListener(new View.OnClickListener() 
  1. 在其中设置用户名和密码
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = et1.getText().toString();
                String s2 = et2.getText().toString();

                if(s1.equals("2663114771") && s2.equals("gzh4869111")){
                    Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();//会在界面下方出现一个灰色的登陆成功的提示
                    Intent intent = new Intent(MainActivity.this,Main2Activity.class);//跳转界面,如果输入正确则跳转到Main2Activity
                    startActivity(intent);
                }else{
                    Toast.makeText(MainActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();
                }
            }
        });

效果:
在这里插入图片描述
在这里插入图片描述

标签:Toast,教程,用户名,界面,安卓,菜鸡,登陆,按钮,组件
来源: https://blog.csdn.net/gzh4869111/article/details/112358168