最近在做一个小项目,然后要求要有登录页面.其实现在大多数app都是有登录页面的.所以就学习了一下怎样实现一个简单的登录页面,然后就写这篇博客和大家分享一下
一个登录页面最基本的需要三个组件,包括两个文本输入框,其中一个是输入账号的,一个是输入密码的,然后当账号和密码都输入完成了之后还有一个登录按钮.这是一个登录界面基本的三个组件.如果你想把它做的更完美,更好看,功能更加完善,还可以加入其他的东西,比如,注册新账号,找回密码等.下面就来具体地实现一个简单的登录页面.
首先是布局文件.使用几个线性布局就行.
<?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:background="@mipmap/login" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <TextView android:id="@+id/loginxx" android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="center" android:layout_marginBottom="30dp" android:layout_marginTop="50dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="50dp" android:background="#ffffffff" android:hint="账号" android:paddingLeft="20dp" android:textColor="#ff000000" android:textSize="14sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#11000000" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="50dp" android:background="#ffffffff" android:hint="密码" android:paddingLeft="20dp" android:textColor="#ff000000" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center"> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:gravity="center" android:text="登 陆" android:textColor="#ffffffff" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="bottom"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:layout_margin="5dp" android:id="@+id/non" android:text="无法登陆?" /> <TextView android:id="@+id/news" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_gravity="bottom|right" android:gravity="left" android:text="新用户" /> </LinearLayout> </LinearLayout>
然后是java代码,因为这里只是实现比较简单的页面,所以没有写数据库什么的验证账号密码什么的,只要求点击登录按钮之后跳转到其他页面就行.那么实现起来也很简单,给button设置一个监听就行.
package com.example.creator.myapplication; /** * Created by creator on 18-6-8. */ import android.annotation.SuppressLint; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.ColorInt; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.content.Intent; public class Login extends AppCompatActivity { private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); login = (Button)findViewById(R.id.login_button); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(Login.this,Welcome.class); startActivity(intent); } }); } }
最后的效果就是这样