android下保存用户账号密码和读取账号密码

private EditText mima;
    private EditText zhanghao;
    private Button button;
    private CheckBox checkbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        zhanghao=(EditText)findViewById(R.id.zhanghao);
        mima=(EditText)findViewById(R.id.mima);
        checkbox=(CheckBox)findViewById(R.id.checkBox);
        Map map=services.servicekey(this);
        if(map!=null){
            zhanghao.setText(map.get("zh"));
            mima.setText(map.get("ma"));
            
        }
        
        
    }
    public void longs(View view){
        String zh=zhanghao.getText().toString().trim();
        String ma=mima.getText().toString().trim();
        if(TextUtils.isEmpty(zh)||TextUtils.isEmpty(ma)){
            Toast.makeText(getApplicationContext(), "密码或账号不能为空", 0).show();
        
        }else{//判断是否保存密码
            if(checkbox.isChecked()){
                //保存密码
                boolean bb =services.serviceText(zh, ma, this);
                if(bb){
                    Toast.makeText(getApplicationContext(), "保存成功", 0).show();
                }
            }
            //登录发送消息到服务器 验证密码是否正确
            if("hejun".equals(zh)&&"4834591".equals(ma)){
                Toast.makeText(getApplicationContext(), "登录成功",0).show();
                
            }else{
                Toast.makeText(getApplicationContext(), "密码错误", 0).show();
                
            }
            
        }
        
        
    }

业务方法


 //保存用户名 密码的业务方法
    // true保存成功 false保存失败

public static boolean serviceText(String zh ,String ma ,Context context){
		
		try {
			File file =new File(context.getFilesDir(),"info.txt");
			FileOutputStream fos =new FileOutputStream(file);
			fos.write((zh+"##"+ma).getBytes());
			fos.close();
			return true;
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return false;
		}
		
		
		
	}
	//获取保存的数据
	public static Mapservicekey(Context context){
		File file =new File(context.getFilesDir(),"info.txt");
		try {
			FileInputStream fis =new FileInputStream(file);
			BufferedReader buffer =new BufferedReader(new InputStreamReader(fis));
			String str =buffer.readLine();
			String[] infos=str.split("##");
			Map map=new HashMap();
		    map.put("zh",infos[0] );
		    map.put("ma", infos[1]);
		    return map;
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}		
		
		
	}
}

利用sharedPreference读取和保存数据

public static void SharedPerfer(Context context,String name,String word){
          SharedPreferences sp =context.getSharedPreferences("shared", context.MODE_PRIVATE);
           Editor edit= sp.edit();
           edit.putString("name", name);
           edit.putString("word", word);
           edit.commit();
           

   SharedPreferences sp= getSharedPreferences("shared", MODE_PRIVATE);
		 String zh=  sp.getString("zh", "");
		 zh.setText(zh);
		  String ma sp.getString("ma", "");
		ma.setText(ma);




你可能感兴趣的:(android)