我是Android上的新编程。 我想要的是通过单击一个Button并保存我在TextView中插入的Text来向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中插入文本时,我希望文本“保存在某个地方”,以便在应用程序的开头可读。对于每个创建的TextView,显然都是这样。
答案 0 :(得分:0)
在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);
答案 1 :(得分:0)
您可以使用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();
}
}