[英]How to use SharedPreferences to save the text entered in edittext and display it in TextView in another Activity
我試圖從活動中保存在edittext中輸入的文本並將其發送到另一個活動,以便它可以顯示在最初不可見的文本視圖中。 所以請幫助我..
這是我的第一個xml。 main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText5"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:inputType="number" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignParentBottom="true"
android:layout_marginLeft="49dp"
android:text="@string/save" />
</RelativeLayout>
這是我的第二個xml。
next.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:text="Medium Text"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
這是我的主要行為代碼。 MainActivity.java
public class MainActivity extends Activity {
public SharedPreferences savedData;
private Button mbtn_save;
private EditText medit_currency;
public String s1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbtn_save=(Button)findViewById(R.id.button1);
medit_currency=(EditText)findViewById(R.id.editText5);
savedData=PreferenceManager.getDefaultSharedPreferences(this);
mbtn_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
s1=medit_currency.getText().toString();
savePreference(s1,s1);
Intent i=new Intent(MainActivity.this,Next.class);
startActivity(i);
}
});
public void savePreference(String key,String value)
{
savedData=getPreferences(MODE_PRIVATE);
SharedPreferences.Editor e=savedData.edit();
e.putString(key, value);
e.commit();
Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
public void showPreference(String key)
{
savedData=getPreferences(MODE_PRIVATE);
String text=savedData.getString(key, "");
}
}}
這是我的第二項活動
Next.java
public class Next extends Activity {
MainActivity ma;
private TextView mtext_one;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
mtext_one=(TextView)findViewById(R.id.textView1);
ma=new MainActivity();
ma.savedData=getPreferences(MODE_PRIVATE);
mtext_one.setVisibility(CONTEXT_INCLUDE_CODE);
mtext_one.setText(ma.savedData.getString("10", ""));
}
}
一旦我保存首選項並移動到另一個活動,我無法獲得Next.java中顯示的文本..幫助我..
在MainActivity.java中,通過intent將值傳遞給第二個活動,如下所示:
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
s1=medit_currency.getText().toString();
savePreference(s1,s1);
Intent i=new Intent(MainActivity.this,Next.class);
// add below line
intent.putExtra("s1", s1);
startActivity(i);
}
在Next.java中,在onCreate中,您可以使用:
Bundle bundle = getIntent().getExtras();
String s1 = bundle.getString("s1");
使用這個:
SharedPreferences sp = getSharedPreferences("Key", Activity.MODE_PRIVATE);
SharedPreferences.Editor edt = sp.edit();
edt.putString("Name", "your Edit Text Value");
edt.commit();
在新活動中檢索:
SharedPreferences sp = getSharedPreferences("Key", Activity.MODE_PRIVATE);
String name = sp.getString("Name", "");
mtext_one.setText(ma.savedData.getString("10", ""));
把你正在使用的密鑰保存起來。 所以,它將是mtext_one.setText(ma.savedData.getString(key, ""));
最好使用intent extras而不是sharedPreference。
以下是使用intent extras的示例。
在您的第一個活動(A)中 :
Intent i = new Intent(A.this, B.class);
i.putExtra("someName", variableThatYouNeedToPass);
startActivity(i);
在你的第二個活動(B)中 :
Bundle extras = getIntent().getExtras();
int fetchedVariable = extras.getInt("someName");
像這樣打開SharedPreference
:
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
這將獲得apps
默認的共享首選項。
用於將文本輸入到sharedPrefernces中的EditText
SharedPreferences spppp = getSharedPreferences("tab", 0);
SharedPreferences.Editor editors = spppp.edit();
editors.putString("for", "you_value");
editors.commit();
為了獲得另一項活動的價值
SharedPreferences spppp = getSharedPreferences("tab", 0);
String get_value = spppp.getString("for" , "");
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.