安卓 实现记住用户名和密码功能

开发步骤

  1. 在程序布局中键入复选框
  2. 在LoginActivity类中加入登陆处理的方法
    3.在 LoginActivity类中加入是否记住密码的判断方法
    4.运用SharedPreferences进行用户名和密码的存储
    5.如果用户名和密码正确,会跳转到欢迎界面

注意问题

  1. 要注意数据之间的转换
  2. 复选框的不同状态处理
  3. SharedPreferences的用法
  4. 页面的正确跳转

事件处理的主要代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
         sp = this.getSharedPreferences("userdata", Context.MODE_WORLD_READABLE); 
          Username = (EditText) findViewById(R.id.etUsername);
          Password = (EditText) findViewById(R.id.etPassword);  
          cbPW =(CheckBox) findViewById(R.id.cbPW);
          btnLogin = (Button) findViewById(R.id.btnLogin);  
          if(sp.getBoolean("IsCheck",true)){  
           //设置默认是记录密码状态  
               cbPW.setChecked(true);  
             Username.setText(sp.getString("UserName", ""));  
             Password.setText(sp.getString("Password", ""));
          }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
       public void onClick(View view) {  
                          UserNameValue = Username.getText().toString();  
                           PasswordValue = Password.getText().toString();                  
                           if(UserNameValue.equals("admin")&&PasswordValue.equals("12345")){  
                              Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();  
                           //登录成功和记住密码框为选中状态才保存用户信息  
                            if(cbPW.isChecked())  
                               {  
                              //记住用户名、密码、  
                                 Editor editor = sp.edit();  
                               editor.putString("UserName", UserNameValue);  
                             editor.putString("Password",PasswordValue);  
                              editor.commit();  
                              }  
                        //跳转界面  
                            Intent intent = new Intent(LoginActivity.this,WelcomeAvtivity.class);  
                            LoginActivity.this.startActivity(intent);  


                         }else{  

                             Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();  
                         }  

                      }
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {  
           if (cbPW.isChecked()) {
               System.out.println("记住密码已选中");  
               sp.edit().putBoolean("IsCheck", true).commit();  
           }else { 
               System.out.println("记住密码没有选中");  
               sp.edit().putBoolean("IsCheck", false).commit();  
           }
       }

源代码

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值