Implementation content:
Write an interface. There are three lines of content are student number, name, class. There are five buttons required: write sd, read sd, clear interface content, write sp, and read sp.
As shown in the figure:
1, activity_main.xml page layout file
<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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="36dp" android:layout_marginTop="34dp" android:text="学号" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView1" android:layout_marginLeft="34dp" android:layout_toRightOf="@+id/textView1" android:ems="10" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="38dp" android:text="姓名" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView2" android:layout_alignLeft="@+id/editText1" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="20dp" android:layout_toLeftOf="@+id/button3" android:text="写sp" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button4" android:layout_alignLeft="@+id/button2" android:text="读sp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button4" android:layout_marginBottom="89dp" android:layout_toLeftOf="@+id/editText2" android:text="write to sd" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button4" android:layout_alignLeft="@+id/editText2" android:layout_marginBottom="17dp" android:text="Clear" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/button3" android:text="读出sd" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button4" android:layout_below="@+id/textView2" android:layout_marginTop="44dp" android:text="class" /> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView3" android:layout_alignRight="@+id/editText2" android:layout_toRightOf="@+id/button1" android:ems="10" /> </RelativeLayout>
The specific layout content is as above, the layout is not perfect, and the interface content is basically realized.
2, MainActivity.java file content
package com.ligang; //import com.dxxy.lab3.R; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; //import com.dxxy.lab2.R; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private Button loginBt,loginBt1,Rdsp,RdBt,clear; private EditText qqNo,qqPsw,banji; SharedPreferences sp;//Data read and write variables Editor edit; //Editor edit object private File sdCardDir;//sd card path private File saveFile;//File name private FileOutputStream outStream;//input file stream private FileInputStream inStream;//input file @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); loginBt1 = (Button) findViewById(R.id.button1); RdBt = (Button) findViewById(R.id.button2); clear =(Button) findViewById(R.id.button3); loginBt = (Button)findViewById(R.id.button4); Rdsp = (Button)findViewById(R.id.button5); qqNo = (EditText)findViewById(R.id.editText1); qqPsw = (EditText)findViewById(R.id.editText2); banji = (EditText)findViewById(R.id.editText3); sp = this.getSharedPreferences("201521111012",MODE_WORLD_READABLE); //Use the edit() method to get the Editor object. edit = sp.edit(); //Monitor the write sp button to complete the xml file writing function loginBt.setOnClickListener(new OnClickListener(){ public void onClick(View v){ if(qqNo.getText().toString().equals("")||qqPsw.getText().toString().equals("")){ Toast toast = Toast.makeText(getApplicationContext(), "Please enter student number and name", Toast.LENGTH_SHORT); toast.show(); } else{ //Store key-value key-value pair data through the Editor object edit.putString("学号",qqNo.getText().toString()); edit.putString("姓名",qqPsw.getText().toString()); edit.putString("班级",banji.getText().toString()); // Submit data through the commit() method edit.commit(); Toast toast = Toast.makeText(getApplicationContext(), "sp write successfully", Toast.LENGTH_SHORT); toast.show(); } } }); //3 Complete the reading function of user name and password Rdsp.setOnClickListener(new OnClickListener(){ public void onClick(View v){ if(null!=sp.getString("学号","")) { qqNo.setText(sp.getString("学号","")); } if(null!=sp.getString("姓名","")) { qqPsw.setText(sp.getString("姓名","")); } if(null!=sp.getString("班级","")) { banji.setText(sp.getString("班级","")); } Toast toast = Toast.makeText(getApplicationContext(), "sp read successfully", Toast.LENGTH_SHORT); toast.show(); } }); //Monitor the write file button to complete the file writing function loginBt1.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (qqNo.getText().toString().equals("") || qqPsw.getText().toString().equals("")) { Toast toast = Toast.makeText(getApplicationContext(), "Please enter student number name", Toast.LENGTH_SHORT); toast.show(); } else { Toast toast = Toast.makeText(getApplicationContext(), "Start writing to file", Toast.LENGTH_SHORT); toast.show(); Write_Files("201521111012.txt",qqNo.getText().toString()); Write_Files("ligang.txt",qqPsw.getText().toString()); Write_Files("dianxin.txt",banji.getText().toString()); /*Toast toast = Toast.makeText(getApplicationContext(), "Write to file successful", Toast.LENGTH_SHORT); toast.show();*/ } } }); //5 Complete the reading function of user name and password RdBt.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(null!=Read_Files("201521111012.txt")) { qqNo.setText(Read_Files("201521111012.txt")); } if(null!=Read_Files("ligang.txt")) { qqPsw.setText(Read_Files("ligang.txt")); } if(null!=Read_Files("dianxin.txt")) { banji.setText(Read_Files("dianxin.txt")); } } }); //Complete the interface content clearing function clear.setOnClickListener(new OnClickListener() { public void onClick(View v) { qqNo.setText(""); qqPsw.setText(""); banji.setText(""); Toast toast = Toast.makeText(getApplicationContext(), "Interface content successfully cleared", Toast.LENGTH_SHORT); toast.show(); } }); } @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; } private void Write_Files(String fileName, String strContent) { sdCardDir = Environment.getExternalStorageDirectory(); saveFile = new File(sdCardDir, fileName); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // create initialization stream object try { outStream = new FileOutputStream(saveFile); } catch (FileNotFoundException e) { Toast.makeText(this, "The file does not exist!", Toast.LENGTH_SHORT).show(); return; } try { // write content to file outStream.write(strContent.getBytes()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block Toast.makeText(this, "No exception found in file!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText(this, "File read and write exception!", Toast.LENGTH_SHORT).show(); } catch (NullPointerException e) { Toast.makeText(this, "File read and write null pointer exception!", Toast.LENGTH_SHORT).show(); } finally { try { // close the file output stream outStream.close(); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText(this, "File read and write exception!", Toast.LENGTH_SHORT).show(); } Toast.makeText(this, "The file was written successfully!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "The memory card does not exist!", Toast.LENGTH_SHORT).show(); return; } } // 3 Complete the file read operation function private String Read_Files(String fileName) { sdCardDir = Environment.getExternalStorageDirectory(); saveFile = new File(sdCardDir, fileName); int len; // Then create a byte array output stream byte[] buffer = new byte[1024]; ByteArrayOutputStream ostream = new ByteArrayOutputStream(); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // create initialization stream object try { inStream = new FileInputStream(saveFile); Log.v("Instance inStream", "实例化inStream!"); } catch (FileNotFoundException e) { Toast.makeText(this, "The file does not exist!", Toast.LENGTH_SHORT).show(); return null; } try { Log.v("Read", "Read data!"); Toast.makeText(this, "The file was read successfully!", Toast.LENGTH_SHORT).show(); while ((len = inStream.read(buffer)) != -1) { ostream.write(buffer, 0, len); } Log.v("Read", "读成功!"); } catch (IOException e) { Toast.makeText(this, "读 异常!", Toast.LENGTH_SHORT).show(); } try { // Finally close the input stream and output stream inStream.close(); ostream.close(); Log.v("Read", "读成功!"); } catch (IOException e) { Toast.makeText(this, "读 异常!", Toast.LENGTH_SHORT).show(); } String str = new String(ostream.toByteArray()); Log.v("Disp", "Text display!"); return str; } else { Toast.makeText(this, "The memory card does not exist!", Toast.LENGTH_SHORT).show(); return null; } } }