文章目录
文件操作
简单的File 操作
因为之前学过sqlite,就不先写了,但是还没有看过文件的读取和保存,只不过在安卓的文件操作和java的操作大差不差。
先写一写简单的布局文件。
main_activity2.xml
main_activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:textSize="25dp"
/>
<EditText
android:id="@+id/editname"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="detailtitle"
android:textSize="25dp"/>
<EditText
android:id="@+id/editdetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btnread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"/>
<Button
android:id="@+id/btnsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"/>
<Button
android:id="@+id/btnclean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除"/>
</LinearLayout>
</LinearLayout>
FileHelper.java
再写一个文件操作类
FileHelper.java
package com.example.myapplication;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileHelper {
private Context mContext;
public FileHelper(){}
public FileHelper(Context mContext) {
super();
this.mContext = mContext;
}
public void save(String filename, String filecontent) throws Exception{
FileOutputStream output = mContext.openFileOutput(filename, Context.MODE_PRIVATE);
output.write(filecontent.getBytes());
output.close();
}
public String read(String filename) throws IOException {
FileInputStream input = mContext.openFileInput(filename);
byte[] temp = new byte[1024];
StringBuffer sb = new StringBuffer("");
int len = 0;
while ((len = input.read(temp)) > 0) {
sb.append(new String(temp,0,len));
}
input.close();
return sb.toString();
}
}
MainActivity2.java
最后就是将Activity的功能进行补全
MainActivity2.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener{
private EditText editname;
private EditText editdetail;
private Button btnsave;
private Button btnclean;
private Button btnread;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mContext = getApplicationContext();
bindView();
}
private void bindView(){
editdetail = (EditText) findViewById(R.id.editdetail);
editname = (EditText) findViewById(R.id.editname);
btnsave = (Button) findViewById(R.id.btnsave);
btnclean = (Button) findViewById(R.id.btnclean);
btnread = (Button) findViewById(R.id.btnread);
btnsave.setOnClickListener(this);
btnclean.setOnClickListener(this);
btnread.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnclean:
editdetail.setText("");
editname.setText("");
break;
case R.id.btnsave:
FileHelper fHelper = new FileHelper(mContext);
String filename = editname.getText().toString();
String filedetail = editdetail.getText().toString();
try {
fHelper.save(filename, filedetail);
Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "数据写入失败", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnread:
String detail = "";
FileHelper fHelper2 = new FileHelper(getApplicationContext());
try {
String fname = editname.getText().toString();
detail = fHelper2.read(fname);
} catch (IOException e) {
e.printStackTrace();
}
editdetail.setText(detail);
Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show();
break;
}
}
}
启动后输入相应的信息并点击保存将信息存储到文件中,再通过title来读取文件(ps:这里的title也就是文件名)并且将其中的内容输出到EditText和广播中。
SharedPreferences 使用
写一个简单的例子,用于记录账号密码。
先写一个界面用于输入账号和密码
activity_main.xml
adtivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录!"
android:textSize="25dp"
android:layout_gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="账号:"/>
<EditText
android:id="@+id/account"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="密码:"
/>
<EditText
android:id="@+id/password"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="30dp"
android:layout_gravity="center"
/>
<Button
android:id="@+id/registered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="30dp"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
SharedHelper.java
写一个简单的SharedHelper工具类
SharedHelper.java
package com.example.testformogq.data;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;
import com.example.testformogq.MainActivity;
import java.util.HashMap;
import java.util.Map;
public class SharedHelper {
private Context myContext;
public SharedHelper(){}
public SharedHelper(Context context){
this.myContext = context;
}
public void save (String username, String password) {
SharedPreferences sharedPreferences = myContext.getSharedPreferences("mysp",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username",username);
editor.putString("password",password);
editor.commit();
Toast.makeText(myContext,"信息已写入sharedPreference中",Toast.LENGTH_SHORT).show();
}
public Map<String, String> read() {
Map<String, String> data = new HashMap<>();
SharedPreferences sp = myContext.getSharedPreferences("mysp",Context.MODE_PRIVATE);
data.put("username",sp.getString("username", ""));
data.put("password",sp.getString("password", ""));
return data;
}
}
MainActivity.java
编写相关的逻辑操作
MainActivity.java
package com.example.testformogq.data;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;
import com.example.testformogq.MainActivity;
import java.util.HashMap;
import java.util.Map;
public class SharedHelper {
private Context myContext;
public SharedHelper(){}
public SharedHelper(Context context){
this.myContext = context;
}
public void save (String username, String password) {
SharedPreferences sharedPreferences = myContext.getSharedPreferences("mysp",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username",username);
editor.putString("password",password);
editor.commit();
Toast.makeText(myContext,"信息已写入sharedPreference中",Toast.LENGTH_SHORT).show();
}
public Map<String, String> read() {
Map<String, String> data = new HashMap<>();
SharedPreferences sp = myContext.getSharedPreferences("mysp",Context.MODE_PRIVATE);
data.put("username",sp.getString("username", ""));
data.put("password",sp.getString("password", ""));
return data;
}
}
输入后点击登录就会将账号密码使用SharedPreferences来进行存储,并且在你下一次打开这个界面时自动填入。