从 TextView 保存文本

Saving text from TextView

我是 Android 的编程新手。 我想要的是通过单击一个按钮并保存我在 TextView 中插入的文本来向 LinearLayout 添加一些 TextView。

这是我的 Java 代码:

public class MainActivity extends AppCompatActivity {
private LinearLayout mLayout;
private EditText mEditText;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLayout = (LinearLayout) findViewById(R.id.linearLayout);
    mEditText = (EditText) findViewById(R.id.editText);
    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(onClick());
    TextView textView = new TextView(this);
    textView.setText("New text");
}

private View.OnClickListener onClick() {
    return new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mLayout.addView(createNewTextView(mEditText.getText().toString()));
        }
    };
}

private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setText("New text: " + text);
    return textView;
}

public void onDestroy(Bundle savedInstanceState) {
}

还有我的 XML 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
<EditText
    android:id="@+id/editText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add+"
    />

TextView 的创建成功了。 因此,当我在顶部 EditText 中插入文本时,我希望将文本保存 "somewhere" 以便在应用程序打开时可读。显然,这适用于创建的每个 TextView。

查看 Android 开发人员指南中的各种 Storage options

针对您的场景。使用 Shared Preferences 可能是有益的,因为您可以将数据存储在键值对中。

在共享首选项中设置数据(在您覆盖的 onClickListener 中):

SharedPreferences settings = getSharedPreferences("AppStorage", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("MyKey",mEditText.getText().toString());
editor.commit();

从共享首选项中获取数据(在您覆盖的 onCreate 中):

SharedPreferences settings = getSharedPreferences("AppStorage", 0);
String data = settings.getString("MyKey", null);

您可以简单地使用 SharedPreferences 来存储和检索文本,您可以使用 下面的代码来完成你的任务,

我在你的 createNewTextView 方法中做了一个小改动,

package com.package.name;

import java.util.ArrayList;

import org.json.JSONArray;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

public class MainActivity extends Activity 
{
    private LinearLayout mLayout;
    private EditText mEditText;
    private Button mButton;

    private View.OnClickListener onClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLayout.addView(createNewTextView(mEditText.getText().toString()));
            }
        };
    }

    //Create a field variable
    ArrayList<String> mCreatedText = new ArrayList<String>();

    private TextView createNewTextView(String text) {
        final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText("New text: " + text);
        mCreatedText.add(text);
        return textView;
    }

    private void savingText() 
    {
        SharedPreferences.Editor mEditor = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
        mEditor.putString("created_text", new Gson().toJson(mCreatedText)).commit();
    }

    private ArrayList<String> getSavedContent()
    {
        ArrayList<String> strArray = new ArrayList<String>();
        SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        try
        {
            String jsonArrayString = mPrefs.getString("created_text","");
            if (!jsonArrayString.equalsIgnoreCase(""))
            {
                JSONArray jsonArray = new JSONArray(jsonArrayString);
                for (int i = 0; i < jsonArray.length(); i++) {
                    strArray.add(jsonArray.getString(i));
                }
            }
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
        return strArray;
    }

    //Call this method from onCreate()
    private void createSavedTextViews()
    {//nhj
        this.mCreatedText = getSavedContent();
        for(int i = 0; i < this.mCreatedText.size(); i++)
        {
            mLayout.addView(restoreAllTextViews(this.mCreatedText.get(i)));
        }
    }
    private TextView restoreAllTextViews(String text)
    {
        final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText("New text: " + text);
        return textView;
    }
    @Override
    protected void onStop() {
        super.onStop();
        savingText();
    }
    //Here is your new onCreate
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rela);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mEditText = (EditText) findViewById(R.id.editText);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(onClick());
        createSavedTextViews();
    }
}