页面登录
来源:互联网 发布:手机提示网络异常 编辑:程序博客网 时间:2024/04/29 07:57
一、建立布局文件:activity_login.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:id="@+id/activity_login" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/loginbg" tools:context="cn.edn.bzu.case_login.LoginActivity"> <include layout="@layout/login_top"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/deer" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="28dp" android:layout_marginEnd="28dp" android:layout_marginBottom="16dp" android:id="@+id/imageView" /></RelativeLayout>
这个布局文件中并没有对相应的控件进行设置,这个页面主要完成背景和样式然后将其他页面加载到此页面中。
Android:background=”@drawable/loginbg”引用了自己设置的背景 loginbg.xml如下
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#FFACDAE5" android:endColor="#FF72CAE1" android:angle="45" /></shape>
- 1
- 2
- 3
注意:<include layout="@layout/login_top"></include>
的作用时将login_top.xml加载到该布局文件中。
二、建立布局文件:login_top.xml
<?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="wrap_content" android:background="@drawable/logintop_roundbg" android:padding="@dimen/activity_horizontal_margin"> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_user" android:hint="@string/etName" > <requestFocus/> </EditText> <EditText android:id="@+id/etPassword" android:layout_below="@id/etName" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_pass" android:hint="@string/etPass" > <requestFocus/> </EditText> <LinearLayout android:layout_below="@id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cbIsRememberPass" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="记住密码" android:textSize="20sp"/> <Button android:id="@+id/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btn_select" android:text="@string/btnLogin" android:onClick="login"/> </LinearLayout></RelativeLayout>
- 1
- 2
这个布局文件主要设置了控件,控件添加了自己的样式相应的布局文件(从上到下)
(1).logintop_roundbg.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp"></corners> <solid android:color="#55FFFFFF"></solid></shape>
- 1
(2).btn_select.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_shape" android:state_pressed="false"/> <item android:drawable="@drawable/btn_shape_after" android:state_pressed="true"/></selector>
- 1
- 2
三、编辑输入的信息正确然后跳转的布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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="cn.edn.bzu.case_login.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome you" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="40sp"/></RelativeLayout>
- 1
- 2
四、在LoginActivity中进行相应的操作
package cn.edn.bzu.case_login;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.ShareActionProvider;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity { private EditText etName; private EditText etPassword; private CheckBox cbIsRememberPass; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initViews(); sharedPreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE); boolean isRemember= sharedPreferences.getBoolean("rememberpassword",false); if(isRemember) { String name=sharedPreferences.getString("name",""); String password=sharedPreferences.getString("password",""); etName.setText(name); etPassword.setText(password); cbIsRememberPass.setChecked(true); } } private void initViews() { etName= (EditText) findViewById(R.id.etName); etPassword= (EditText) findViewById(R.id.etPassword); cbIsRememberPass= (CheckBox) findViewById(R.id.cbIsRememberPass); } public void login(View view) { String name=etName.getText().toString(); String password=etPassword.getText().toString(); if("admin".equals(name)&&"123456".equals(password)) { SharedPreferences.Editor editor=sharedPreferences.edit(); if(cbIsRememberPass.isChecked()) { editor.putBoolean("rememberpassword",true); editor.putString("name",name); editor.putString("password",password); } else { editor.clear(); } editor.commit(); Intent intent=new Intent(this,MainActivity.class); startActivity(intent); finish(); }else { Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show(); } }}
五、登录成功跳转到MainActivity
package cn.edn.bzu.case_login;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
六、打开虚拟机的Android Device Monitor阅读全文
0 0
- 登录页面
- 登录页面
- 登录页面
- 登录页面
- 登录页面
- 登录页面
- 页面登录
- 登录页面
- 登录页面
- 跳转页面+登录页面
- 登录页面(login)
- javascript 弹出登录页面
- javascript 弹出登录页面
- 论坛登录页面
- struts简单登录页面
- 登录页面引导页
- 做完登录页面了
- 页面登录进度条
- python name 'file' is not defined的解决办法
- git/github 学习笔记
- HM代码的使用
- Javascript模块化编程
- APP安全报告第21期:体育竞技类手游比其他手游更安全,更重视代码保护!
- 页面登录
- 【经典算法】:蛇形填数
- Java IO详解——随机访问文件流
- Opengl 4种光照模型
- php封装类模仿链式操作
- 初学安卓-通过URL加载网络图片
- scrollWidth,clientWidth,offsetWidth等属性的理解
- 爬取网站前2_支持代理
- 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。