如何使用共享preferences来保存的EditText输入的文本并显示它的TextView在另一个活动 [英] How to use SharedPreferences to save the text entered in edittext and display it in TextView in another Activity

查看:178
本文介绍了如何使用共享preferences来保存的EditText输入的文本并显示它的TextView在另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在EditText上输入的文字从活动中保存和发送到另一个活动,以便它可以在一个TextView这是无形的最初显示。所以,请帮助我..

I am trying to save the text entered in edittext from an activity and send it to another activity so that it can be displayed in a textview which is invisible at first. So pls help me..

这是我的第一个XML。
main.xml中

This is my first 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。

This is my second 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>

这是我的主要actvity code。
   MainActivity.java

This is my main actvity code. 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, "");
}
}}

这是我的第二次​​活动。

This is my second activity

Next.java

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", ""));
}



}

我不能让我一次保存preference并移动到另一个活动..帮我Next.java显示文本。

I cant get the text displayed in Next.java once i save the preference and move to another activity.. help me..

推荐答案

在MainActivity.java,通过意向如下传递价值,第二项活动

In MainActivity.java, pass the value to second activity through the intent as below:

@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,你可以使用:

In Next.java, in onCreate, you can use:

Bundle bundle = getIntent().getExtras();
String s1 = bundle.getString("s1");

这篇关于如何使用共享preferences来保存的EditText输入的文本并显示它的TextView在另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆