简体   繁体   English

Android-如何将值从活动保存到Java类

[英]Android - How to save values from activity to java class

My problem is that I need to save the values from a EditText from an Activity in a Java .class file to get that value if I reopen the same Activity. 我的问题是,如果我重新打开同一Activity,则需要将来自Activity的EditText的值保存在Java .class文件中,以获取该值。 I already searched around some forums and all I could find is about Intent I am still learning Android.. 我已经在一些论坛上搜索了,所有我能找到的都是关于Intent的信息,而我仍在学习Android。

EDIT 编辑

I was looking for something like this: This class have the variables; 我正在寻找这样的东西:此类具有变量;

public class SharedActivity { 公共类SharedActivity {

public static String convite; 公共静态字符串转换;

} }

In activity 1 I do this: 在活动1中,我这样做:

SharedActivity.convite=EditText value;

In activity 2 I get the value from SharedActivity.convite variable to an EditText, but the value that I get is null, any ideas? 在活动2中,我从SharedActivity.convite变量获得了一个EditText的值,但是我得到的值为null,有什么想法吗? Thanks! 谢谢!

Maybe SharedPreferences is what you are looking for 也许您正在寻找SharedPreferences

Put this snippet inside any Event handler method eg Button 's OnClickListener : 将此代码段放入任何事件处理程序方法中,例如ButtonOnClickListener

//creating an instance of a shared preference with code 'edit_text_code' and only reachable by this application
SharedPreferences mySharedPrefs=getSharedPreferences("my_shared_prefs_file_name",MODE_PRIVATE);
//getting the mySharedPrefs's editor for further editing
SharedPreferences.Editor editor=settings.edit();
//putting the EditText content as a shared preference to be 'commited'
editor.putString("edit_text_code",myEditText.getText().toString());
editor.commit();

And now you should read the "shared preference" and its preferences inside, in this case, your EditText content: 现在,您应该在内部阅读“共享首选项”及其首选项,在这种情况下,您的EditText内容为:

eg 例如

public class MainActivity2 extends AppCompatActivity {
TextView myTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        myTextView = (TextView) findViewById(R.id.my_text_view);
        SharedPreferences mySharedPrefs = getSharedPreferences("my_shared_prefs_file_name",MODE_PRIVATE);
        String content = mySharedPrefs.getString("edit_text_code","Show this text in case that shared preference doesn't exist");
        textView.setText("The EditText content was: "+  content);
    }

For further learning on more topics. 有关更多主题的进一步学习。 Check out this blog: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html 查看此博客: http : //www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html

Try this ChalkSreet app about Android Development also: https://play.google.com/store/apps/details?id=com.chalkstreet.learnandroid&hl=en There you can find interesting examples including one very similar to your question 也可以尝试以下有关Android开发的ChalkSreet应用: https : //play.google.com/store/apps/details? id=com.chalkstreet.learnandroid &hl=zh_CN在这里您可以找到有趣的示例,其中包括一个与您的问题非常相似的示例

应用程序屏幕截图1

应用程式萤幕撷取2

EDIT 编辑

Here is the source of the example I was talking about and it Should work. 这是我所讨论的示例的来源,它应该可以工作。

public class SharedActivity extends AppCompatActivity {
    EditText et;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_shared);
        et = (EditText) findViewById(R.id.edit_text_shared);
        SharedPreferences settings = getSharedPreferences("PREFS",MODE_PRIVATE);
        et.setText(settings.getString("option",""));
    }

    //This does the trick    
    @Override
    protected void onStop() {
        super.onStop();
        SharedPreferences settings = getSharedPreferences("PREFS",0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("option",et.getText().toString());
        editor.commit();
    }
}

OR You can create a file using Java OutputStream for writing and InputStream classes to store that "variable" 或者,您可以使用Java OutputStream编写文件,并使用InputStream类存储该“变量”

You can put your data into SharedPreferences when the Activity stops (override the onStop method) like this 您可以在Activity停止(覆盖onStop方法)时将数据放入SharedPreferences中,如下所示

SharedPreferences data = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = data.edit();
      editor.putString("edit_text_data", editText.getText().toString());
      editor.commit();

And when you restore (start) your Activity, you can retrieve the data like this: 当您恢复(开始)活动时,可以像这样检索数据:

SharedPreferences data = getSharedPreferences(PREFS_NAME, 0);
       String editData = data.getString("edit_text_data", "none"); 
//The second parameter is default value if it doesnt find that tag.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM