将文本编辑为永久
edit text to permanent
大家好,我需要此代码的帮助。假设我有下面的 Textview "Phone Owner" 和 EditText "Jerry Smith"(etJerrySmith 是 editable ="true")
所以我 运行 应用程序,我可以将 "Jerry Smith" 更改为任何名称,但是当我关闭并再次 运行 时,它将 returns 更改为 "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();
}
}
我编辑了它,现在这是我的代码
您应该在单击按钮或关闭 activity 时将新输入的值保存在共享首选项中,并且当您打开应用程序时,在此 activity 的 onCreate() 中您应该从共享首选项中读取值,然后将其设置回编辑文本。通过这种方式,您将能够为上述编辑文本设置新值。
如果您使用按钮,代码将类似于:
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 上。
希望对您有所帮助。
大家好,我需要此代码的帮助。假设我有下面的 Textview "Phone Owner" 和 EditText "Jerry Smith"(etJerrySmith 是 editable ="true")
所以我 运行 应用程序,我可以将 "Jerry Smith" 更改为任何名称,但是当我关闭并再次 运行 时,它将 returns 更改为 "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();
}
}
我编辑了它,现在这是我的代码
您应该在单击按钮或关闭 activity 时将新输入的值保存在共享首选项中,并且当您打开应用程序时,在此 activity 的 onCreate() 中您应该从共享首选项中读取值,然后将其设置回编辑文本。通过这种方式,您将能够为上述编辑文本设置新值。
如果您使用按钮,代码将类似于:
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 上。
希望对您有所帮助。