登录 File
应用私有路径 data/data/包名/
控件:CheckBox isChecked();
MainActivity.java
public class MainActivity extends Activity {private EditText et_username;private EditText et_pwd;private CheckBox cb_isSave;private Button btn_login;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//加载界面setContentView(R.layout.activity_main);//找到关心的控件et_username = (EditText) findViewById(R.id.et_username);et_pwd = (EditText) findViewById(R.id.et_password);cb_isSave = (CheckBox) findViewById(R.id.cb_isSave);btn_login = (Button) findViewById(R.id.btn_login);//设置点击事件btn_login.setOnClickListener(new MyListener());//获取用户保存的信息// String[] info = Utils.readInfo();// String[] info = Utils.readInfobyContext(this);String[] info = Utils.readInfoFromSdCard();//如果返回不为空 说明有信息 显示到edittext上if(info!=null){//显示用户的信息et_username.setText(info[0]);et_pwd.setText(info[1]);}}private class MyListener implements OnClickListener{@Overridepublic void onClick(View v) {//当按钮被点击就会走这个方法//①获取用户输入String pwd = et_pwd.getText().toString().trim();String username = et_username.getText().toString().trim();//②判断输入是否为空if(TextUtils.isEmpty(username)||TextUtils.isEmpty(pwd)){//2.1如果为空 Toast提示用户 不能为空Toast.makeText(MainActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();}else{//2.2如果不为空 判断是否保存密码//③ 通过checkbox的状态 判断是否保存boolean checked = cb_isSave.isChecked();if(checked){//boolean saveInfo = Utils.saveInfo(username,pwd);//boolean saveInfo = Utils.saveInfobycontext(MainActivity.this,username,pwd);boolean saveInfo = Utils.saveInfo2sdcard(username,pwd);if(saveInfo){Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();}//勾选上了 保存用户名密码//Log.d("MainActivity", "保存用户名:"+username+"密码:"+pwd);}//④执行登陆的业务逻辑Log.d("MainActivity", "开始登陆....");}}}}
Utils.java
/*** 保存用户名密码* @param username 用户名* @param pwd 密码* @return 是否保存成功*/public static boolean saveInfo(String username, String pwd) {String info = username+"##"+pwd;File file = new File("data/data/com.itheima.logindemo/info.txt");try {FileOutputStream fos = new FileOutputStream(file);fos.write(info.getBytes());fos.close();return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 获取用户保存的用户名和密码* @return 数组的第一个元素是用户名 第二个元素是密码 如果为null说明获取失败*/public static String[] readInfo(){File file = new File("data/data/com.itheima.logindemo/info.txt");try {FileInputStream fis = new FileInputStream(file);BufferedReader reader = new BufferedReader(new InputStreamReader(fis));String temp = reader.readLine();String[] result = temp.split("##");return result;} catch (Exception e) {e.printStackTrace();return null;}}
布局文件
<RelativeLayout 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: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=".MainActivity" ><EditTextandroid:id="@+id/et_username"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入用户名" /><EditTextandroid:id="@+id/et_password"android:layout_below="@id/et_username"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="textPassword"android:hint="请输入密码"/><CheckBoxandroid:id="@+id/cb_isSave"android:layout_below="@id/et_password"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="勾选保存信息"/><Buttonandroid:id="@+id/btn_login"android:layout_below="@id/et_password"android:layout_alignParentRight="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="登陆"/></RelativeLayout>