简体   繁体   English

Android即使在应用程序终止时也如何保存editText的值

[英]Android How to save the value of editText even when the app is terminated

I made this app which the user inputs the school name, and it will link to the website. 我制作了这个应用,用户输入了学校名称,它将链接到该网站。 So, I want to know how to save the school name, so you don't have to put it in every time you open the app. 因此,我想知道如何保存学校名称,因此您不必每次打开应用程序时都将其放入。 Thank you. 谢谢。

My app contains an edittext, a go button (Which I want to use it as a save also), and a webview. 我的应用程序包含一个edittext,一个go按钮(我也想将其用作保存对象)和一个webview。

我建议您查看http://developer.android.com/training/basics/data-storage/index.html对于这一点,这里只存储一个字符串,我将使用SharedPreference来查找您需要在培训指南上;)

you can save the value using something like this: 您可以使用以下方式保存值:

final EditText eActNametwo = (EditText)findViewById(R.id.eACTNametwo);
    final EditText eActBudtwo = (EditText)findViewById(R.id.eACTBudtwo);

    bCreatetwo = (Button)findViewById(R.id.bCreateActivitytwo);
    bCreatetwo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SharedPreferences settingstwo = getSharedPreferences(actnamefiletwo,0);
            SharedPreferences.Editor editortwo = settingstwo.edit();
            editortwo.putString("nametwo", eActNametwo.getText().toString());
            editortwo.putString("budtwo", eActBudtwo.getText().toString());
            editortwo.commit();
        }
    });

and then u call the value somewhere using this 然后你用这个在某个地方调用值

entvActNametwo = (TextView) findViewById(R.id.tvActNametwo);
    tvActBudtwo = (TextView) findViewById(R.id.tvActBudtwo);
    SharedPreferences settingstwo = getSharedPreferences(actnamefiletwo, 0);
    tvActNametwo.setText(settingstwo.getString("nametwo", ""));
    tvActBudtwo.setText(settingstwo.getString("budtwo", ""));

you will need to call your string prior to onCreate like this: 您需要像这样在onCreate之前调用您的字符串:

public static final String actnamefiletwo = "actnamebudtwo";

This is an example from my own app, hope it helps 这是我自己的应用程序中的一个示例,希望对您有所帮助

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

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