【问题标题】:Need help to save/restore a string between activities需要帮助来保存/恢复活动之间的字符串
【发布时间】:2013-04-24 06:15:29
【问题描述】:

我正在学习 Android,但在网上没有找到答案。我想在我的MainActivity 上保存来自EditText 的字符串,以便在我的第三个Activity -> Activity3 上恢复它 我希望这个字符串显示在这个ActivityTextView 上。

public class MainActivity extends Activity {

  private EditText nomPrenom;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nomPrenom = (EditText) findViewById(R.id.nomPrenom);
 ...................}

在我的 activity_main.xml 中:

android:id="@+id/nomPrenom"
android:text="@string/nomPrenom"

public class Activity3 extends ListActivity {

private TextView yourName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity3);
    yourName = (TextView) findViewById(R.id.nomPrenom);
    yourName.setText("take the name of activity 1 here"); //<-- this line causes crash !!


..................}

这里是activity3.xml

android:id="@+id/yourName"
android:text="@string/nomPrenom" 

我该怎么做?

【问题讨论】:

  • 把你的主要活动的整个代码。
  • 你能发布错误信息吗?
  • 尝试共享首选项..
  • 已解决 谢谢大家

标签: android string android-activity save restore


【解决方案1】:

首先你的问题不是很清楚。但根据给定,我认为你需要从edittext(在主要活动中)获取数据,然后将其传递给第三个活动。

为此,您需要使用 Intent 将数据从一个活动传递到另一个活动。

              Example  - 
              nomPrenom = (EditText) findViewById(R.id.nomPrenom);
              nomPrenom .getText.toString();

             Intent in = new Intent(MainActivity.this, Activity3 .class);
                    in.putExtra("value",nomPrenom .getText.toString()) ;

在 Activity3 中接收这样的意图:-

                       String strValue = getIntent().getExtras().getString("value");
                       yourName = (TextView) findViewById(R.id.yourName);
                       yourName.setText(strValue);

【讨论】:

    【解决方案2】:

    使用Intent.putExtra("Your text here") 并将其传递给其他活动。

    或者您可以从EditText 中获取字符串并生成字符串Globalpublicstatic,然后在任何其他类中使用它。

    【讨论】:

      【解决方案3】:

      您可以通过多种方式做到这一点...

      您可以使用共享首选项,以便您可以从任何地方访问它。但在您的申请关闭后,共享偏好将保留您的价值。

      当您像这样创建活动时,通过活动意图传递您的价值的另一种方式:intent.putExtra' and get value into your activity viagetExtra`

      您可以在全局位置定义变量的另一种方法是考虑创建一个类来保存此类公共变量,然后您可以从任何地方访问这些变量。

      这三种方式都可以根据您的要求进行更改。

      【讨论】:

        【解决方案4】:

        为了从 MainActivity 移动到 Activity3,编写以下代码

        //get your edittext's text here
        String text = nomPrenom.getText().toString();
        
        //move to Activity3
        Intent intent = new Intent(MainActivity.this, Activity3.class);
        //pass the edit text value to Activity3
        intent.putExtra("EditTextValue", text);
        //start Activity3
        startIntent(intent);
        

        在 Activity3 中,要获取您的编辑文本值,请执行以下操作。

        //get your edit text value here which is passed in MainActivity
        String text = getIntent().getStringExtra("EditTextValue");
        

        变量“text”是您要查找的最终输出。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多