简体   繁体   English

Android更改值存储在程序中

[英]Android changed value stored in program

I have a dialog where the user have to write the password. 我有一个对话框,用户必须在其中输入密码。 If it is appropriate then will open another a dialog where the user can change some settings. 如果合适,则将打开另一个对话框,用户可以在其中更改某些设置。 Is there any why to save the changed value in to exactly to the program or I have to write in to file and realoaded from there. 有什么理由将更改后的值准确地保存到程序中,还是我必须写入文件并从那里重新加载。 But I think it is not so safty if I want to hide this settings from simple users. 但是,我想对简单用户隐藏此设置并不是那么安全。

Which is the best solutions for that? 哪个是最好的解决方案?

As long as you store a hashed version of the password there shouldn't be a problem. 只要您存储密码的哈希版本,就不会有问题。

When you check the password you then check against the hashed version. 当您检查密码时,然后对照哈希版本进行检查。

Whether you choose to store it in a flat file, or database doesn't matter, for me if it was just a couple of users I would be tempted by some config file, if multiple users might use the same device I might use a database. 无论您选择将其存储在平面文件中还是数据库中都没有关系,对我来说,如果它只是几个用户,那么我会被一些配置文件所吸引,如果多个用户可能使用同一设备,那么我可能会使用数据库。 It also depends if my users are going to have other associated data. 这也取决于我的用户是否将拥有其他关联数据。

You can use SharedPrefernces for this purpose. 您可以为此目的使用SharedPrefernces Or, you can try Storage Options provided by Android. 或者,您可以尝试Android提供的存储选项

Edit 编辑
This code allows you to enter some text in EditText and when you click Save it will be saved. 此代码使您可以在EditText输入一些文本,然后单击“ Save将被保存。 The saved text would be displayed in EditText when the app is opened again. 再次打开应用程序时,保存的文本将显示在EditText

MainActivity.java MainActivity.java

package pcsalt.example.sharedprefdemo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText et;
    Button btn;
    SharedPreferences pref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btn);
        pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                editor = pref.edit();
                String text = et.getText().toString();
                editor.putString("key_value", text);
                editor.commit();
            }
        });

        et.setText(pref.getString("key_value", "default value"));
    }

}

activity_main.xml activity_main.xml

<LinearLayout 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"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Some text" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM