Android 为什么每次我更改SharedReferences的值时,它都不会更新?

Android 为什么每次我更改SharedReferences的值时,它都不会更新?,android,sharedpreferences,Android,Sharedpreferences,我正在制作一个加密应用程序。 问题是我在一个共享按钮中转换了操作设置按钮。 所以我想要的是: 每次有人在EditText中写入(例如:Hello)并单击加密按钮时,该字符串存储在SharedReferences中,并准备在WhatsApp、Telegram中共享。。(这是由shareactionprovider制作的)。 但是在我的例子中,如果你一个接一个地加密两个单词,这并不重要。 它只存储第一个 我如何更改它,以便它可以共享已编写的最后一个单词 代码如下: public class Main

我正在制作一个加密应用程序。 问题是我在一个共享按钮中转换了操作设置按钮。 所以我想要的是: 每次有人在EditText中写入(例如:Hello)并单击加密按钮时,该字符串存储在SharedReferences中,并准备在WhatsApp、Telegram中共享。。(这是由shareactionprovider制作的)。 但是在我的例子中,如果你一个接一个地加密两个单词,这并不重要。 它只存储第一个

我如何更改它,以便它可以共享已编写的最后一个单词

代码如下:

public class MainActivity extends AppCompatActivity {
Button borrar, encriptar, desencriptar;
EditText palabra;
String  cambio;
TextWatcher tt = null;
int i;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    encriptar = (Button) findViewById(R.id.btnencriptar);
    desencriptar = (Button) findViewById(R.id.btndesencriptar);
    palabra = (EditText) findViewById(R.id.edittext1);


    encriptar.setOnClickListener(new View.OnClickListener() { //button that encrypts
        @Override
        public void onClick(View v) {

            String texto = palabra.getText().toString(); //string taken from edittext






            texto = texto.replace("a", "9R"); //changes "a" with "9R", the same with the rest of the alphabet is below
            texto = texto.replace("b", "9G");
            texto = texto.replace("c", "9B");

            texto = texto.replace("d", "8R");
            texto = texto.replace("e", "8G");
            texto = texto.replace("f", "8B");

            texto = texto.replace("g", "7R");
            texto = texto.replace("h", "7G");
            texto = texto.replace("i", "7B");

            texto = texto.replace("j", "6R");
            texto = texto.replace("k", "6G");
            texto = texto.replace("l", "6B");

            texto = texto.replace("m", "5R");
            texto = texto.replace("n", "5G");
            texto = texto.replace("ñ", "5B");

            texto = texto.replace("o", "4R");
            texto = texto.replace("p", "4G");
            texto = texto.replace("q", "4B");

            texto = texto.replace("r", "3R");
            texto = texto.replace("s", "3G");
            texto = texto.replace("t", "3B");

            texto = texto.replace("u", "2R");
            texto = texto.replace("v", "2G");
            texto = texto.replace("w", "2B");

            texto = texto.replace("x", "1R");
            texto = texto.replace("y", "1G");
            texto = texto.replace("z", "1B");




            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            SharedPreferences.Editor editor = preferences.edit();

            editor.putString("Name", texto);

            editor.apply();

            palabra.setText(texto);


        }
    });
}





        public void desencriptar(View v) { //this converts 9R to a, 9G to b...

            String texto2 = palabra.getText().toString();
            texto2 = texto2.replace("9R", "a");
            texto2= texto2.replace("9G", "b");
            texto2 = texto2.replace("9B", "c");





            palabra.setText(texto2);

        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) { // here is the share button that shares the stored value


            getMenuInflater().inflate(R.menu.menu_main, menu);
            MenuItem item = menu.findItem(R.id.menu_share);
            ShareActionProvider
                    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
            // Create the share Intent
            Intent shareintent = new Intent(Intent.ACTION_SEND);
            shareintent.setAction(Intent.ACTION_SEND);
            shareintent.setType("text/plain");

            SharedPreferencespreferences=PreferenceManager.getDefaultSharedPreferences(this);//the problem maybe is here where I load the string value
            String name = preferences.getString("Name", "");

            shareintent.putExtra(Intent.EXTRA_TEXT, name);

            mShareActionProvider.setShareIntent(shareintent);


            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.menu_share) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }
这里是xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/edittext1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="85dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ENCRIPTAR"
    android:id="@+id/btnencriptar"
    android:layout_below="@+id/edittext1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DESENCRIPTAR"
    android:id="@+id/btndesencriptar"
    android:onClick="desencriptar"
    android:layout_alignTop="@+id/btnencriptar"
    android:layout_alignRight="@+id/edittext1"
    android:layout_alignEnd="@+id/edittext1" />


非常感谢。

我只想问:为什么不使用正确的加密?尝试从按钮单击移动共享首选项初始化代码,使其成为全局代码,并且在代码中使用
SharedReferences
有什么原因吗?我不明白你想实现什么。你不需要它。我想我需要它来传递字符串来共享它。我错了吗?您是否试图在同一个键上存储多个值?因为那是行不通的。