安卓记住密码

安卓开发中,我们做用户登录的时候需要将用户名和密码保存,下次打开的时候记住应户名和密码,也就是记住用户名和密码。关于记住用户名和密码的保存方式有多种,这里介绍SharaedPreferenses保存方式。
这种保存方式主要是以XML的格式来保存文件的,在开发中的explorer中可以看见。下面看一个小例子,主要是代码,希望对开发者有所帮助:
实现的主要代码:
private EditText username,password;
private static final String FILE_NAME="saveUserNamePwd";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_name = (EditText)this.findViewById(R.id.username);
pwd = (EditText) this.findViewById(R.id.password);
SharedPreferences sharedPreferences = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
//从文件中获取保存的数据
String usernameContent = sharedPreferences.getString("username", "");
String passwordContent = sharedPreferences.getString("password", "");
//判断是否有数据存在,并进行相应处理
if(usernameContent != null && !"".equals(usernameContent))
user_name.setText(usernameContent);
if(passwordContent != null && !"".equals(passwordContent))
pwd.setText(passwordContent);
}
@Override
protected void onSaveContent() {
super.onStop();
String usernameContent = username.getText().toString();
String passwordContent = password.getText().toString();
//获取SharedPreferences时,需要设置两参数
//第一个是保存的文件的名称,第二个参数是保存的模式(是否只被本应用使用)
SharedPreferences sharedPreferences =
getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
//添加要保存的数据
editor.putString("username", usernameContent);
editor.putString("password", passwordContent);
//确认保存
editor.commit();
}
这个比较简单,主要是SharaedPreferences的应用。在onCreate中的代码是获取保存的数据,并显示在用户名密码框中,onSaveContent主要是记住用户名和密码,也就是保存用户名密码信息到相关文件中,对这两个的合理使用,就可以实现记住用户名和密码。

 ================




几乎所有应用都会涉及到登录界面,那么就会有相应的记住密码和自动登录选项。我主要采用了SharedPreference来实现,代码比较简单。如果有什么不对的地方也希望指正,第一次发帖哈O(∩_∩)O!<ignore_js_op> Longin.rar (162.19 KB, 下载次数: 964)
下面是main.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/chat_bg_default"
    >
    <TextView 
    android:layout_height="50dp"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:textSize="24sp"
    android:text="登录界面"
    android:textColor="@color/white"
    android:background="@drawable/song_index_item_bg"/>
    <ImageView 
    android:id="@+id/iv"
    android:layout_height="220dp"
    android:layout_width="230dp"
    android:layout_marginTop="110dp"
    android:src="@drawable/background"
    android:layout_centerHorizontal="true"
    android:scaleType="fitXY"/>
    <TextView 
    android:id="@+id/tv_name"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textSize="15sp"
    android:layout_marginTop="7dp"
    android:text="账号:"
    android:layout_alignLeft="@+id/iv"
    android:layout_alignTop="@+id/iv"
    android:layout_marginLeft="7dp"
    android:textColor="@color/green"/>
    <EditText 
    android:id="@+id/et_name"
    android:layout_height="25dp"
    android:layout_width="210dp"
    android:layout_marginTop="7dp"
    android:layout_below="@+id/tv_name"
    android:background="@drawable/shape"
    android:layout_alignLeft="@+id/tv_name"/>
    <TextView 
    android:id="@+id/tv_pass"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textSize="15sp"
    android:text="密码:"
    android:layout_alignLeft="@+id/tv_name"
    android:layout_below="@+id/et_name"
    android:layout_marginTop="7dp"
    android:textColor="@color/green"/>
    <EditText 
    android:id="@+id/et_pass"
    android:layout_height="25dp"
    android:layout_width="210dp"
    android:background="@drawable/shape"
    android:layout_marginTop="7dp"
    android:layout_below="@+id/tv_pass"
    android:layout_alignLeft="@+id/tv_name"/>
    <CheckBox 
    android:id="@+id/isremenber"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignLeft="@+id/tv_name"
    android:layout_marginTop="10dp"
    android:text="记住密码"
    android:textSize="11sp"
    android:textColor="@color/huise"
    android:layout_below="@+id/et_pass"
    android:button="@drawable/mycheckbox"/>
    <CheckBox 
    android:id="@+id/isloginself"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignRight="@+id/et_name"
    android:layout_marginTop="10dp"
    android:text="自动登录"
    android:textSize="11sp"
    android:textColor="@color/huise"
    android:layout_below="@+id/et_pass"
    android:button="@drawable/mycheckbox"/>
    <Button 
    android:id="@+id/longin"
    android:layout_height="25dp" 
    android:layout_width="210dp"
    android:layout_below="@+id/isloginself"
    android:layout_alignLeft="@+id/et_pass"
    android:layout_marginTop="10dp"
    android:text="登录"
    android:gravity="center"
    android:background="@drawable/loginbtn"/>
</RelativeLayout>
主activity也比较简单 直接上代码:
public class LonginActivity extends Activity {
        private EditText name;
        private EditText pass;
        private CheckBox isRemenber;
        private CheckBox isLoginSelf;
        private Button longin;
        private ProgressDialog mDialog;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        name=(EditText)findViewById(R.id.et_name);
        pass=(EditText)findViewById(R.id.et_pass);
        isRemenber=(CheckBox)findViewById(R.id.isremenber);
        isLoginSelf=(CheckBox)findViewById(R.id.isloginself);
        longin=(Button)findViewById(R.id.longin);

        final SharedPreferences pre=getSharedPreferences("longinvalue", MODE_WORLD_WRITEABLE);
        if(pre!=null){
                //记住了密码
           if(pre.getBoolean("isrmb", false)==true){
                   name.setText(pre.getString("name", null));
                   pass.setText(pre.getString("pass", null));
                   isRemenber.setChecked(true) ;
           }
           if(pre.getBoolean("islgs", false)==true){
                   isLoginSelf.setChecked(true);
                   creatDialog();
                   new Thread(){
                           public void run() {
                                   try {

                                                Thread.sleep(3000);
                                        if(mDialog.isShowing()){
                                              mDialog.dismiss();
                                      }
                                                Intent intent2=new Intent(LonginActivity.this,Show.class);
                                                startActivity(intent2);
                                        } catch (Exception e) {
                                                // TODO: handle exception
                                        }
                           }
                   }.start();
           }
        }
        isRemenber.setOnCheckedChangeListener( new OnCheckedChangeListener() {
                        
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                // TODO Auto-generated method stub
                              if(isRemenber.isChecked()==false){
                                      isLoginSelf.setChecked(false);
                              }
                        }
                });
        isLoginSelf.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                // TODO Auto-generated method stub
                                  isRemenber.setChecked(true);
                        }
                });
        longin.setOnClickListener(new OnClickListener() {
                        
                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if(!name.getText().toString().equals("")&&!pass.getText().toString().equals("")){
                                        if(isLoginSelf.isChecked()){
                                                pre.edit().putBoolean("isrmb", true).putBoolean("islgs", true).putString("name", name.getText().toString())
                                                .putString("pass", pass.getText().toString()).commit();
                                        }else{
                                                if(isRemenber.isChecked()){
                                                        pre.edit().putBoolean("isrmb", true).putBoolean("islgs", false).putString("name", name.getText().toString())
                                                        .putString("pass", pass.getText().toString()).commit();
                                                }else{
                                                        pre.edit().putBoolean("isrmb", false).putBoolean("islgs", false).putString("name", name.getText().toString())
                                                        .putString("pass", pass.getText().toString()).commit();
                                                }
                                        }
                                        Intent intent=new Intent(LonginActivity.this,Show.class);
                                        startActivity(intent);
                                }else{
                                        Toast.makeText(getApplicationContext(), "密码或账号不能为空!", Toast.LENGTH_LONG).show();
                                }

                        }
                });
    }
    private void creatDialog() {
                // TODO Auto-generated method stub
              mDialog=new ProgressDialog(this);
              mDialog.setTitle("验证中");
              mDialog.setMessage("正在登陆请稍后");
              mDialog.setIndeterminate(true);
              mDialog.setCancelable(true);
              mDialog.show();

        }
}<ignore_js_op>
点击登录之后就进入第二个activity ,最后使用SharedPreference 需要设置一下权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>就这么多啦,希望大家都能设计一个漂亮的登录界面!

原文地址:https://www.cnblogs.com/awkflf11/p/4346336.html