android实现QQ登录界面(大学作业一)

实验项目:

QQ登录界面

实验地点:

躬行楼718

实验时间:

2018.10.13

一、实验目的:

1.掌握Android中布局的概念和用法

2.熟练掌握Android中Button、ImageView、EditText以及Toast的基本使用。

3. 熟练掌握偏好设置的用法

二、实验内容与要求

1.完成如下所示的QQ登录界面

2.功能需求:

2.1 界面需要做简单屏幕适配(weight属性)

2.2 用户名明文显示且只能是数字

2.3 密码必须是密文显示,字符数字都可以。

2.4 用户名或密码空,点击登录提示"用户名密码不能为空"

2.5 用户名和密码为指定时,点击按钮提示登录成功。

2.6 登录成功后,将用户名和密码保存在偏好设置中,

2.7 退出QQ再次打开,记住用户名密码并显示出来

三、实验步骤和结果:

用户名是123,密码是password

输入验证成功后即可显示登陆成功提示,然后保存到偏好设置中,以后开启用户名和密码就会显示在文本框内

main_activity.xml:

<android.support.constraint.ConstraintLayout 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=".MainActivity">

<LinearLayout xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/background_gradient"

android:orientation="vertical">

<ImageView

android:id="@+id/imageView1"

android:layout_width="160dp"

android:layout_height="0dp"

android:layout_gravity="center_horizontal"

android:layout_marginTop="10dp"

android:layout_marginBottom="5dp"

android:layout_weight="1"

android:src="@drawable/logo" />

<LinearLayout

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:orientation="vertical">

<EditText

android:id="@+id/username"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_below="@id/imageView1"

android:layout_marginTop="5dp"

android:background="#ffffff"

android:hint="QQ/手机号"

android:inputType="number"

android:paddingLeft="12dp"

android:textColor="#000000" />

<EditText

android:id="@+id/pwd"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_below="@id/username"

android:layout_marginTop="5dp"

android:background="#ffffff"

android:hint="密码"

android:inputType="textPassword"

android:paddingLeft="12dp"

android:textColor="#000000" />

<Button

android:id="@+id/login"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_below="@id/pwd"

android:layout_marginLeft="20dp"

android:layout_marginTop="20dp"

android:layout_marginRight="20dp"

android:background="@drawable/shape"

android:gravity="center"

android:text="登录"

android:textColor="#F9FAFB" />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:padding="10dp"

android:gravity="bottom">

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="无法登录"

android:textColor="#0EB1EF" />

<TextView

android:id="@+id/textView3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="right"

android:text="新用户"

android:textColor="#0EB1EF" />

</LinearLayout>

</LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java主要代码:

package com.lgqchinese.homework;

import …

public class MainActivity extends AppCompatActivity {

private SharedPreferences sp;

private SharedPreferences.Editor editor;

private EditText userEdit;

private EditText passEdit;

private Button login;

String userNameValue;

String passwordValue;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

viewInfo();

/*//隐藏title

if (getSupportActionBar() != null){

getSupportActionBar().hide();

}*/

sp = getSharedPreferences("userInfo", 0);

String name = sp.getString("USER_NAME", "");

String pass = sp.getString("PASSWORD", "");

userEdit.setText(name);

passEdit.setText(pass);

login.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

userNameValue = userEdit.getText().toString();

passwordValue = passEdit.getText().toString();

//用户名或密码空,点击登录提示"用户名密码不能为空"

if(userNameValue.equals("")||passwordValue.equals("")){

Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();

}else {

if (userNameValue.equals("123")&&passwordValue.equals("password")){

//用户名和密码为指定时,点击按钮提示登录成功。

Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

// 登录成功后,将用户名和密码保存在偏好设置中

SharedPreferences.Editor editor = sp.edit();

//保存用户名和密码

editor.putString("USER_NAME", userNameValue);

editor.putString("PASSWORD", passwordValue);

editor.commit();

}

}

}

});

}

private void viewInfo() {

userEdit = (EditText) findViewById(R.id.username);

passEdit = (EditText) findViewById(R.id.pwd);

login = (Button) findViewById(R.id.login);

}

}

四、实验总结:

通过实验主要学习sharedpreferences类,逐渐理解其理,掌握使用方法。理解源码,以便更好地掌握。

昔日我曾苍老,如今风华正茂(ง •̀_•́)ง
原文地址:https://www.cnblogs.com/lgqrlchinese/p/9923062.html