Android-登录界面

Android:登录界面

前端界面(布局文件) :相对布局+组件:ImageView,EditText,TextView
java代码:Toast显示消息,edtUserName.getText().toString()—>转变成字符串,setOnClickListener(按钮的监听事件)
消息提示:Toast.makeText(MainActivity.this,“用户名不正确”,Toast.LENGTH_LONG).show();

布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/rtLytTop"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="30px"
        >
        <ImageView
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/qq7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </RelativeLayout>

    <RelativeLayout
    android:id="@+id/rtLytMiddle"
    android:layout_width="match_parent"
    android:layout_height="50dp"
        android:layout_below="@+id/rtLytTop"
        android:layout_alignParentStart="true">

    <EditText
        android:id="@+id/edtUserName"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:hint="请输入QQ号"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:scaleType="center"
        android:src="@mipmap/ic_launcher" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/lyMiddle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_below="@+id/rtLytMiddle"
        android:layout_alignParentStart="true">

        <EditText
            android:id="@+id/edtPassWd"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:hint="请输入密码" />
    </LinearLayout>


<Button
    android:id="@+id/btnlogin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="登录"
    android:layout_below="@+id/lyMiddle"/>

    <RelativeLayout
        android:id="@+id/rtLytEnd"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/txt1"
            android:paddingLeft="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="无法登录?"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true" />


        <TextView
            android:id="@+id/txt2"
            android:paddingLeft="340dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="新用户"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true" />

        </RelativeLayout>

</RelativeLayout>

Java代码
package com.example.qq.myloginapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    EditText edtUserName,edtPassWd;
    Button btnlogin;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.relative_layout);
        //初始化组件
        edtUserName = (EditText) findViewById(R.id.edtUserName);
        edtPassWd = (EditText) findViewById(R.id.edtPassWd);
        btnlogin = (Button) findViewById(R.id.btnlogin);

        //登录按钮的监听事件
        btnlogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String userName = edtUserName.getText().toString();
                String passWd = edtPassWd.getText().toString();
                if (!"qq".equals(userName)) {
                    Toast.makeText(MainActivity.this,"用户名不正确", Toast.LENGTH_LONG).show();
                }
                if (!"123".equals(passWd)) {
                    Toast.makeText(MainActivity.this, "密码不正确", Toast.LENGTH_LONG).show();
                }
                if ("qq".equals(userName) && "123".equals(passWd)) {
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}
运行效果图

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android Studio 登陆界面的代码主要包括以下几个方面: 1. 布局文件:登陆界面的布局文件通常采用 XML 格式,通过布局文件中的各种控件来实现 UI 界面的展示和交互。 2. 控制器代码:登陆界面需要与用户进行交互,通过控制器代码来实现与布局文件的交互逻辑。 3. 模型代码:登陆界面需要进行数据的验证和存储,通过模型代码来实现对数据的操作。 下面是一个简单的登陆界面代码示例: 1. 布局文件 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"> <EditText android:id="@+id/editText_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" /> <EditText android:id="@+id/editText_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> <Button android:id="@+id/button_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Log in" /> </LinearLayout> ``` 2. 控制器代码 LoginController.java ``` public class LoginController extends AppCompatActivity { private EditText mEditTextEmail; private EditText mEditTextPassword; private Button mButtonLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mEditTextEmail = findViewById(R.id.editText_email); mEditTextPassword = findViewById(R.id.editText_password); mButtonLogin = findViewById(R.id.button_login); mButtonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String email = mEditTextEmail.getText().toString(); String password = mEditTextPassword.getText().toString(); // TODO: 进行数据验证 // TODO: 进行登陆操作 } }); } } ``` 3. 模型代码 LoginModel.java ``` public class LoginModel { private String mEmail; private String mPassword; public LoginModel(String email, String password) { mEmail = email; mPassword = password; } public boolean isEmailValid() { // TODO: 进行邮箱格式验证 return true; } public boolean isPasswordValid() { // TODO: 进行密码格式验证 return true; } public boolean login() { // TODO: 进行登陆操作 return true; } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值