模仿QQ记住账号和密码的登陆与实现的android开发实例

1.配置布局/res/layout/activity_main.xml



    

    

    

    
2.程序代码块/记住密码/src/com/example/jizhumima/MainActivity.java

package com.example.jizhumima;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText et_username;
	private EditText et_password;
	private CheckBox cb_jizhumima;
	private SharedPreferences config;
	private boolean isChecked;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		config=getSharedPreferences("config", MODE_PRIVATE);
		et_username=(EditText) findViewById(R.id.et_username);
		et_password=(EditText) findViewById(R.id.et_password);
		cb_jizhumima=(CheckBox) findViewById(R.id.cb_jizhumima);
		//是否记住了密码
		boolean isCheched=config.getBoolean("isChecked",true);
		if (isChecked){
		et_username.setText(config.getString("username",""));
		et_password.setText(config.getString("password",""));
		}
		cb_jizhumima.setChecked(isChecked);
	}	
	//登陆事件
	public void onClick(View view){
		Editor edit=config.edit();
		String username=et_username.getText().toString();
		String password=et_password.getText().toString();
		boolean isChecked=cb_jizhumima.isChecked();
		//存储CheckBox的状态
		edit.putBoolean("isChecked",isChecked);
		if(isChecked){
		edit.putString("username",username).putString("password",password);
		}else{
		edit.remove("username").remove("username");
		}
		//之前是在内存里,提交到本地
		edit.commit();
		Toast.makeText(this,"登陆成功",0).show();
	}
}
3.运行效果

后期再加截图了,AVD竟然启动不来了.


你可能感兴趣的:(开发案例)