Background
We use a common CheckBox to show whether to save the account and password when logging on.
The last step is to save the txt file and use Java I/O operations.
This is just a rough one.
Save files
Public static void savefile2card (Context context, String username, String password) {File file = null; FileOutputStream fos = null; try {// file = new File ("/data/com. yuyidong. savefile/savefile.txt "); file = new File (context. getFilesDir (), "info.txt"); fos = new FileOutputStream (file); fos. write (username + "!!!! "+ Password ). getBytes ();} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace (); try {fos. close ();} catch (IOException e1) {// catch Block e1.printStackTrace () generated automatically by TODO ();}}}
Read files
Public static Map <String, String> getSaveFile (Context context) {File file = new File (context. getFilesDir (), "info.txt"); try {FileInputStream FCM = new FileInputStream (file); BufferedReader br = new BufferedReader (new InputStreamReader (FCM); String str = br. readLine (); String [] infos = str. split ("!!!! "); Map <String, String> map = new HashMap <String, String> (); map. put ("username", infos [0]); map. put ("password", infos [1]); br. close (); return map;} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace (); return null ;}finally {}}
Main Program
public class MainActivity extends Activity { private Button button; private CheckBox check; private EditText usernameText; private EditText passwordText; private String username; private String password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); check = (CheckBox) findViewById(R.id.check);
usernameText = (EditText) findViewById(R.id.username); passwordText = (EditText) findViewById(R.id.password);
button.setOnClickListener(new buttonListener());
Map <String, String> map = read. getSaveFile (this); usernameText. setText (map. get ("username"); passwordText. setText (map. get ("password");} class buttonListener implements OnClickListener {@ Override public void onClick (View v) {// The method stub username = usernameText automatically generated by TODO. getText (). toString (); password = passwordText. getText (). toString (); System. out. println (username + "~!! ~ "+ Password); if (check. isChecked () {save. savefile2card (MainActivity. this, username, password );}}}
I am the dividing line of tiantiao
Source code: http://pan.baidu.com/s/1dD1Qx01
SaveFile.zip
Reprinted please indicate the source: http://www.cnblogs.com/yydcdut/p/3708964.html