安卓开发练习【用户登录】

学习Android 开发第一天,做了这么一个小界面。

登录界面

在这里插入图片描述

布局资源

头像

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:gravity="center">

        <ImageView
            android:layout_width="400px"
            android:layout_height="400px"
            android:src="@mipmap/user_img" />
    </LinearLayout>

输入框

用户名

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="100px"
                android:layout_height="100px"
                android:src="@mipmap/xiao_user" />

            <EditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/user_name"
                android:singleLine="true" />
        </LinearLayout>

密码

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="100px"
                android:layout_height="100px"
                android:src="@mipmap/yao_shi" />

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/user_password"
                android:inputType="textPassword"
                android:singleLine="true" />
        </LinearLayout>

按钮

登录

            <Button
                android:id="@+id/login"
                android:layout_width="250px"
                android:layout_height="150px"
                android:layout_marginRight="50px"
                android:background="@drawable/button"
                android:text="@string/login"
                android:textSize="60px" />

退出

            <Button
                android:id="@+id/back"
                android:layout_width="250px"
                android:layout_height="150px"
                android:background="@drawable/button"
                android:text="@string/back"
                android:textSize="60px" />

主布局界面

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private Button login;
    private Button back;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源设置用户界面
        setContentView(R.layout.activity_main);
        //获取控件实例
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        back = findViewById(R.id.back);
        //为登录按钮设置监听,实现监听器接口,编写事件处理方法
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户输入的信息
                String input_username = username.getText().toString().trim();
                String input_password = password.getText().toString().trim();
                // 判断用户是否登录成功
                if (input_username.equals("wen") && input_password.equals("123")) {
                    Toast.makeText(MainActivity.this, "登录成功了", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_LONG).show();
                    // 清空用户名和密码
                    username.setText("");
                    password.setText("");
                    // 用户名获取焦点
                    username.requestFocus();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 关闭当前窗口
                finish();
            }
        });

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值