栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android数据存储:SharedPreferences 登录账号

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android数据存储:SharedPreferences 登录账号

前言:

笔记:此次简单的登录是对课本中的数据存储进行练习,比较简单;其中圆形图片是学习他人所得,具体忘记是谁看了太多就不@了OvO。
演示效果:

一、布局文件
首先上效果图



    
    
    

        

        
    

    

        

        

    

    
二、ImageRound类和SpSaveAccount类

自己创建的类,imageRound继承AppCompatImageView,SpSaveAccount封装了保存账号,获取账号两个类,方便使用。

1、ImageRound类
public class ImageRound extends AppCompatImageView {

        笔画
    private Paint imagePaint;
    //    圆形半径
    private int imageRadius;
    //    圆形的缩放比例
    private float imageScle;

    public ImageRound(Context context) {
        super(context);
    }

    public ImageRound(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageRound(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width=getMeasuredWidth();
        int height=getMeasuredHeight();

        int size=Math.min(width,height);
        imageRadius=size/2;
        setMeasuredDimension(size,size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        imagePaint=new Paint();
        Drawable drawable=getDrawable();
        if(drawable!=null){
            Bitmap bitmap=drawableToBitmap(getDrawable());

//            BitmapShader(传入bitmap对象,X轴的平铺模式,y轴的平铺模式)
//            平铺模式:Shader.TileMode.CLAMP:如果着色器超出原始边界范围,会复制边缘颜色
            BitmapShader bitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
//            计算缩放比例
            imageScle=(imageRadius*2.0f)/Math.min(bitmap.getHeight(),bitmap.getWidth());
            Matrix matrix=new Matrix();
            matrix.setScale(imageScle,imageScle);
            bitmapShader.setLocalMatrix(matrix);
            imagePaint.setShader(bitmapShader);
//          drawCircle方法(圆心的x坐标,圆心的y坐标,圆半径,画笔):绘制圆形
            canvas.drawCircle(imageRadius,imageRadius,imageRadius,imagePaint);

        }else {
            super.onDraw(canvas);
        }

    }

    //    drawable转BitMap的方法
    private Bitmap drawableToBitmap(Drawable drawable){
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable=(BitmapDrawable) drawable;
            return bitmapDrawable.getBitmap();
        }
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);
        return bitmap;
    }
}
2、SpSaveAccount类
public class SpSaveAccount {

    //    保存账号
    public static void saveUserInfo(Context context,String account,String password){
        SharedPreferences sharedPreferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("userId",account);
        editor.putString("password",password);
        editor.commit();
    }
    //    读取账号
    public static Map getUserInfo(Context context){
        SharedPreferences sharedPreferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
        String account=sharedPreferences.getString("userId",null);
        String pwd=sharedPreferences.getString("password",null);

        Map userMap=new HashMap();
        userMap.put("account",account);
        userMap.put("password",pwd);
        return userMap;
    }
}
三、MainActivity
public class MainActivity extends AppCompatActivity {

    private Button btnLogin;
    private EditText etName,etPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etName=findViewById(R.id.et_account);
        etPwd=findViewById(R.id.et_password);
        btnLogin=findViewById(R.id.btn_login);
//      登录账号
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String account=etName.getText().toString().trim();
                String pwd=etPwd.getText().toString();

//                账户输入如果为空
                if(TextUtils.isEmpty(account)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(MainActivity.this,"账号密码输入不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                SpSaveAccount.saveUserInfo(MainActivity.this,account,pwd);
                Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            }
        });
//        获取账号并将其显示出来
        Map userMap=SpSaveAccount.getUserInfo(MainActivity.this);
        if(userMap!=null){
            etName.setText(userMap.get("account"));
            etPwd.setText(userMap.get("password"));
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/940770.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号