将文本编辑为永久

时间:2016-02-21 05:11:11

标签: java android text edit

大家好,我需要帮助才能获得此代码。让我们说例如我在Textview“Phone Owner”和EditText“Jerry Smith”之下(etJerrySmith是 editable =“true”

所以我运行应用程序,我可以将“Jerry Smith”更改为任何名称,但当我关闭并再次运行时,它将返回“Jerry Smith”。有没有办法将“杰里·史密斯”变成“Meeseeks”并保持永久性?

content_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">



<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText1"

    android:layout_marginTop="130dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
   />

 <Button

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:onClick="saveName"/>

</RelativeLayout>

MainActivity.java

package com.lowellnc.edittopermanent;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


EditText editText1;

public static final String MyPREFERENCES = "MyPrefs";
public static final String Name = "nameKey";
SharedPreferences sharedpreferences;
String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    editText1 = (EditText) findViewById(R.id.editText1);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    name = settings.getString("name", "USE_ANY_DEFAULT_NAME_HERE");
    editText1.setText(name);
}

public void saveName(View v) {
    SharedPreferences.Editor editor = sharedpreferences.edit();
    String n = editText1.getText().toString();
    editor.putString(Name, n);
    editor.commit();
}

}

我编辑了它,现在是我的代码

1 个答案:

答案 0 :(得分:0)

您应该在点击按钮或关闭活动时将新输入的值保存在共享首选项中,当您打开应用程序时,在此活动的onCreate()内,您应该从共享首选项中读取值,然后设置它回来编辑文字。这样,您就可以为上面提到的edittext设置新值。

如果您使用按钮,代码将类似于:

public class MainActivity extends AppCompatActivity {

EditText editText1;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
SharedPreferences sharedpreferences;
String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    editText1 = (EditText)findViewById(R.id.editText1);
    name = sharedpreferences.getString("nameKey", "USE_ANY_DEFAULT_NAME_HERE");
    editText1.setText(name);
}

public void saveName(View v){
    SharedPreferences.Editor editor = sharedpreferences.edit();
    String n = editText1.getText().toString();
    editor.putString(Name, n);
    editor.commit();
    Toast.makeText(MainActivity.this,"Hey, value you entered is saved", Toast.LENGTH_SHORT).show();
}

您可以在布局中创建一个按钮,并在该按钮的xml的onClick上设置saveName()。然后,无论何时按下按钮,它都会获取edittext的值并将其保存在共享首选项中。现在,无论何时再次打开应用程序,onCreate()都会从共享首选项中读取值并将其设置在edittext上。

希望这会对你有所帮助。