您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
15_android下的数据持久化,保存数据到rom文件
发布时间:2021-02-06 16:45:11编辑:雪饮阅读()
首先实现最基本的布局
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dip"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="用户名"
android:gravity="center"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/et_name"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dip"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:gravity="center"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_pwd"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dip"
android:orientation="horizontal">
<Button
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:id="@+id/bt_login"
/>
<CheckBox android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:checked="false"
android:id="@+id/cb_remember_pwd"
/>
</RelativeLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
.java入口类在同一个路径中用于提供向rom系统中保存数据(保存数据到rom中的文件中)
package com.example.myapplication;
import android.content.Context;
import android.util.Log;
import java.io.FileOutputStream; public class SavePwdService {
//定义应用程序的上下文 没有被初始化
private Context context;
public SavePwdService(Context context) {
this.context = context;
}
/**
* 保存用户密码到手机rom的文件里面
* @param name
* @param pwd
*/
public void saveToRomFile(String name ,String pwd){
// 私有的权限 创建一个 config.txt的文件 并且获取到他的输出流
try {
Log.i("test","正在输出到文件");
FileOutputStream fos = context.openFileOutput("config.txt", Context.MODE_PRIVATE);
String content = name+":"+pwd;
fos.write(content.getBytes());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后完善MainActivity.java入口程序,实现click事件在登陆处调用,该事件中对上面我们SavePwdService中提供的saveToRomFile方法调用即可存储数据到rom的文件中了
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtName;
private EditText mEtPwd;
private Button mBtLogin;
private CheckBox mcb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtName=(EditText) this.findViewById(R.id.et_name);
mEtPwd=(EditText) this.findViewById(R.id.et_pwd);
mBtLogin=(Button) this.findViewById(R.id.bt_login);
mcb=(CheckBox) this.findViewById(R.id.cb_remember_pwd);
mBtLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
if(mcb.isChecked()){
//保存用户名密码到持久化设备
SavePwdService service = new SavePwdService(this);
String name = mEtName.getText().toString().trim();
String pwd = mEtPwd.getText().toString().trim();
service.saveToRomFile(name, pwd);
Toast.makeText(this, "保存成功....", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "登陆中....", Toast.LENGTH_LONG).show();
}
break;
}
}
}接下来就是测试成果了
关键字词:android,rom