欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AS简单的登陆界面

程序员文章站 2022-06-13 13:19:39
...

先设计好界面

<?xml version="1.0" encoding="utf-8"?>
<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"        //保持控件在中心
    android:orientation="vertical"        //
    android:background="@mipmap/loginbgi">   //设置背景图片

    <TextView
        android:id="@+id/Welcome"       
        android:text="@string/Welcome"        //在string里面设置文字,Welcome就代表
                                               //”welcome my farm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">   //默认水平分布

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/usernamecase" />   

        <EditText
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/piusername"
            android:inputType="text" />     //输入类型
    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/userpasswordcase" />

        <EditText
            android:id="@+id/userpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/piuserpassword"
            android:inputType="textPassword"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Login"
            tools:ignore="ButtonStyle" />

        <Button
            android:id="@+id/Regist"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/Regist"
            tools:ignore="ButtonStyle" />

    </LinearLayout>
</LinearLayout>

MainActivity

package com.example.organic_farm_production_management;

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


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private static final String KEY_INDEX = "index";
    private EditText musername;
    private EditText muserpassword;
    private Button mLogin;
    private Button mRegist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        musername = (EditText) findViewById(R.id.username) ;
        muserpassword = (EditText) findViewById(R.id.userpassword);
        mLogin = (Button) findViewById(R.id.Login);
        mRegist = (Button) findViewById(R.id.Regist);

        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//以下是连接数据库
//UserService是有增删改查的一个类,记得换成自己的
                String name = musername.getText().toString();
                if (name.length() == 0)
                {
                    Toast.makeText(MainActivity.this,R.string.nousername,Toast.LENGTH_SHORT).show();
                }else{
                    String password = muserpassword.getText().toString();
                    UserService userService = new UserService(MainActivity.this);
                    boolean flag = userService.login(name,password);
                    if (flag == false)
                    {
                        Toast.makeText(MainActivity.this,R.string.errorlogin,Toast.LENGTH_SHORT).show();
                    }
                    if (flag == true)
                    {
                        Intent intent = new Intent(MainActivity.this,Glebe_Manage_Activity.class);
                        startActivity(intent);
                    }
                }

            }
        });
        mRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,RegistActivity.class);
                startActivity(intent);
            }

        });


    }
}

结果图

AS简单的登陆界面

相关标签: 登陆界面