0

我有 3 个 EditText。当我输入一些文本或一些数字然后单击保存按钮时,它会转到另一个活动。再次回到editText,值消失并设置为android:text="value"。在 editText 框中键入后,我需要显示这些值。

代码:

et=(EditText)findViewById(R.id.pieces);

        et1=(EditText)findViewById(R.id.portions);

        et2=(EditText)findViewById(R.id.ml);

        save=(Button)findViewById(R.id.submit_data);

save.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                SharedPreferences preferences = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);

             SharedPreferences.Editor editor = preferences.edit();
                editor.putString("text1",pieces.getText().toString());
                editor.putString("text2",portions.getText().toString());
                editor.putString("text3",ml.getText().toString());

                editor.commit();



            String oneedit= preferences .getString("text1", "");
            String  twoedit= preferences .getString("text2", "" );
            String  thirdedit= preferences .getString("text3", "" );


            pieces.setText(oneedit);
            portions.setText(twoedit);
            ml.setText(thirdedit);

            /*
         SharedPreferences.Editor editor = preferences.edit();
            editor.putString("text1",pieces.getText().toString());
            editor.putString("text2",portions.getText().toString());
            editor.putString("text3",ml.getText().toString());

            editor.commit();


                Intent intent=new Intent(Activity.this,Activity1.class);
                startActivity(intent);

            }

    });

如果我使用下面的代码,它会加载保存的值:

edit.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0);

                //edittext.setText(sharedpreference.getString(KEYNAME, "No value Stored"));

                et.setText(preferences.getString("text1", " "));
                et1.setText(preferences.getString("text2", " "));
                et2.setText(preferences.getString("text3", " "));

            }


        });
4

1 回答 1

0

像这样尝试我们调用的这条线create method并使用整个活动

  SharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0);

save.setOnClickListener()

   save.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


           SharedPreferences.Editor editor = SharedPreferences .edit();
            editor.putString("text1",et.getText().toString());
            editor.putString("text2",e1t.getText().toString());
            editor.putString("text3",et2.getText().toString());

            editor.commit();

              et.setText("");
         et1.setText("");
         et2.setText("");  
       }
});

这是 onClick 方法

     edit.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub               

         String oneedit= SharedPreferences .getString("text1", null);
         String twoedit= SharedPreferences .getString("text2", null );
         String threeedit= SharedPreferences .getString("text3", null );

         et.setText(oneedit);
         et1.setText(twoedit);
         et2.setText(threeedit);     

        }
    });
于 2013-09-19T18:34:46.510 回答