QQ登录
来源:互联网 发布:mac ios模拟器 编辑:程序博客网 时间:2024/04/30 01:57
1:创建一个名为“QQ登录”的应用程序。设计用户交界面。
布局文件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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E6E6E6" android:orientation="vertical" tools:context="com.edu.bzu.cn.qq.MainActivity"> <ImageView android:layout_width="50dp" android:layout_height="50dp" app:srcCompat="@drawable/aa" android:id="@+id/iv_head" android:layout_centerHorizontal="true" android:layout_marginTop="40dp"/> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_below="@id/iv_head" android:background="#ffffff" android:orientation="vertical" > <RelativeLayout android:id="@+id/r1_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" > <TextView android:text="账号" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:id="@+id/tv_name" /> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/tv_name" android:background="@null"/> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#E6E6E6"/> <RelativeLayout android:id="@+id/r1_userpsw" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:text="密码" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:id="@+id/tv_psw" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/tv_psw" android:background="@null"/> </RelativeLayout> </LinearLayout> <Button android:text="登录" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/layout" android:layout_centerHorizontal="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="20dp" android:id="@+id/btn_login" android:background="#3C8DC4" android:textColor="#ffffff" /></RelativeLayout>
2创建工具类,添加一个utils包,在该包中编写一个Utils类,用于实现QQ号码和密码的存储与获取功能。Utils代码如下:
public class Utils { public static boolean saveUserInfo(Context context,String number,String password) { SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE); SharedPreferences.Editor edit = sp.edit(); edit.putString("userName", number); edit.putString("pwd", password); edit.commit(); return true; } public static Map<String,String>getUserInfo(Context context){ SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE); String number=sp.getString("userName",null); String password=sp.getString("pwd",null); Map<String,String> userMap=new HashMap<String, String>(); userMap.put("number",number); userMap.put("password",password); return userMap; }}
3:编写界面交互代码MainActivity
在MainActivity中,实现当用户输入完QQ号码和密码后,选择记住密码,单击“登录”按钮时调用Utils.saveUserInfo()方法保存QQ密码
package com.edu.bzu.cn.qq;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.Toast;import com.edu.bzu.cn.qq.utils.Utils;import java.util.Map;public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText etNumber; private EditText etpassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); Map<String, String> userInfo = Utils.getUserInfo(this); if (userInfo != null) { etNumber.setText(userInfo.get("number")); etpassword.setText(userInfo.get("password")); } } private void initView() { etNumber = (EditText) findViewById(R.id.et_number); etpassword = (EditText) findViewById(R.id.et_password); findViewById(R.id.btn_login).setOnClickListener(this); } public void onClick(View v){ String number=etNumber.getText().toString().trim(); String password=etNumber.getText().toString(); if(TextUtils.isEmpty(number)) { Toast.makeText(this, "请输入QQ号码", Toast.LENGTH_SHORT).show(); return; } if(TextUtils.isEmpty(password)) { Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show(); return; } Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); Log.i("MainActivity","记住密码:"+number+","+password); boolean isSaveSuccess=Utils.saveUserInfo(this,number,password); if(isSaveSuccess) { Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } }}
4:运行程序登录QQ:
阅读全文
0 0
- QQ登录
- QQ登录
- qq登录
- QQ登录
- QQ登录
- QQ登录
- QQ登录
- QQ登录
- QQ登录
- QQ登录
- QQ登录
- QQ登录
- QQ登录
- qq登录
- qq登录
- QQ快捷登录(sdk登录)
- QQ游戏登录效果
- QQ登录界面
- JAVA之Tomcat基础:Tomcat for linux 安装使用
- 两个链表的第一个公共结点
- 机器学习-->集成学习-->GBDT,RandomForest
- 手机与单片机NFC通讯方案,手机NFC通讯方案,单片机NFC通信方案
- JAVA Mac Maven项目搭建及一些初级配置
- QQ登录
- 数字在排序数组中出现的次数
- 求次数
- 两种方法实现Java读写文件
- android开发艺术探索 学习笔记(一) Activity生命周期全面分析
- 腾讯社交广告大赛总结
- 30 分钟掌握 ES6 / ES2015 核心内容(上)
- Netty构建一个简单的http服务
- 学习笔记:shell基本语法