Android基础--首选项(SharedPreferences) - Go语言中文社区

Android基础--首选项(SharedPreferences)


     

       首选项就是存储软件的配置信息

     在window系统中是以 ini为后缀,然而在android中就是以 xml为后缀。

      这次讲首选项以编写一个登入之后记住密码为例。


 布局:activity_main.xml

<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.huse.edu.sp.MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户名" />
     <EditText
         android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
       <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="密码" />
     <EditText
         android:id="@+id/et_pwd"
         android:password="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
     <LinearLayout 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">
         <CheckBox 
             android:id="@+id/cb_remeber"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="记住密码"/>
          <CheckBox 
             android:id="@+id/cb_autologin"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
              android:layout_weight="1"
             android:text="自动登入"/>
     </LinearLayout>
        <LinearLayout 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">
         <Button 
             android:id="@+id/bt_register"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="注册"/>
          <Button 
             android:id="@+id/bt_login"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
              android:layout_weight="1"
             android:text="登入"/>
     </LinearLayout>

</LinearLayout>
MainActivity.java

package cn.huse.edu.sp;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText et_name;
	private EditText et_pwd;
	private CheckBox cb_remeber;
	private CheckBox cb_autologin;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//获取首选项
		sp = getSharedPreferences("login", Context.MODE_PRIVATE);
		//初始化函数
		initView();
		
		//回显数据
		boolean remeberpwd = sp.getBoolean("remeberpwd", false);
		boolean autologin = sp.getBoolean("autologin", false);
		//记住密码
		if(remeberpwd){
			//获取用户名和密码
			String name = sp.getString("name", "");
			String pwd = sp.getString("pwd", "");
			et_name.setText(name);
			//将光标放到字符最后面
			et_name.setSelection(name.length());
			et_pwd.setText(pwd);
			et_pwd.setSelection(pwd.length());
			//将记住密码勾选上
			cb_remeber.setChecked(true);
		}
		if(autologin){
			//将自动登入勾选上
			cb_autologin.setChecked(true);
			Toast.makeText(this, "自动登入......", 1).show();
		}
	}

	private void initView() {
		//找到控件
		et_name = (EditText) findViewById(R.id.et_name);
		et_pwd = (EditText) findViewById(R.id.et_pwd);
		cb_remeber = (CheckBox) findViewById(R.id.cb_remeber);
		cb_autologin = (CheckBox) findViewById(R.id.cb_autologin);
		Button bt_register = (Button) findViewById(R.id.bt_register);
		Button bt_login = (Button) findViewById(R.id.bt_login);
		
		//监听事件
		MyOnClickListener l = new MyOnClickListener();
		//为按钮添加监听
		bt_register.setOnClickListener(l);
		bt_login.setOnClickListener(l);
	}
	
	private class MyOnClickListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.bt_register:
				//注册按钮事件
				break;
			case R.id.bt_login:
				//登入
				//获取用户名和密码
				String name = et_name.getText().toString().trim();
				String pwd = et_pwd.getText().toString().trim();
				
				if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)){
					//用户名或密码为空
					Toast.makeText(MainActivity.this, "用户名或密码为空", 0).show();
					return;
				}else{
					if(cb_remeber.isChecked()){
						//勾选了记住密码 保存用户名和密码在首选项中
						Editor edit = sp.edit();    //编辑器
						edit.putBoolean("remeberpwd", true);
						edit.putString("name", name);
						edit.putString("pwd", pwd);
						//保存之后必须提交
						edit.commit();
					}
					if(cb_autologin.isChecked()){
						//勾选自动登入
						Editor edit = sp.edit();
						edit.putBoolean("autologin", true);
						edit.commit();
					}
				}
				break;
			}
		}
	}
}
运行后结果截图:





再次运行后结果截图:



     所有说,我们可以利用首选项实现qq的自动登入和记住密码这两个功能的。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/itqingliang/article/details/62438147
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-27 23:17:55
  • 阅读 ( 896 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢