我有一个程序可以保存一个带有高分的.txt文件:
// Create a file to write to.
string createHighscore = _higscore + Environment.NewLine;
File.WriteAllText(path, createText);
// Open the file to read from.
string createHighscore = File.ReadAllText(path);
Run Code Online (Sandbox Code Playgroud)
问题是用户可以使用texteditor尽可能简单地编辑文件.所以我想让文件不可读/不可编辑或加密它.
我的想法是我可以将数据保存在资源文件中,但是我可以在资源文件中写入吗?或者将其保存为.dll,加密/解密或查找MD5-sum/hash.
对于我在 Android 中的最新项目,我需要从 myTextView中设置一个R.layout.header(我的 NavDrawer 的内容,请参阅下面的屏幕截图) MainActivity,它使用activity_main布局。
为此,我调用我的SetUsername()方法,其中包含以下代码以从首选项中获取用户名:
private void SetUsername(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String userNamePref = preferences.getString("username", "DEFAULT");
//Change the Username in R.layout.header
}
Run Code Online (Sandbox Code Playgroud)
要设置用户名,我首先做了
TextView username = (TextView)findViewById(R.id.usernameHeader);
username.setText(userNamePref);
Run Code Online (Sandbox Code Playgroud)
这不起作用 - 显然,因为R.id.usernameHeader位于R.layout.header中。
所以我用了这个:
setContentView(R.layout.header);
TextView username = (TextView)findViewById(R.id.usernameHeader);
username.setText(userNamePref);
Run Code Online (Sandbox Code Playgroud)
这正确地改变了我的 NavDrawer 中的文本,但是activty_main布局消失了,所以我setContentView(R.layout.activity_main)在函数的底部添加了。一切都按预期进行,但文本没有改变。
为了获得更好的想法,我制作了一个屏幕截图,以便您可以看到我到底想要更改什么:

我的布局:
活动_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" …Run Code Online (Sandbox Code Playgroud)