我正在开发一个带有动态视图的应用。 您可以在edittext中编写,然后点击按钮将edittext中的文本添加到textview。与textview相反的是用于删除textview的按钮。现在我想这样做,以便在应用程序关闭时textview将保存。 我用共享偏好和许多其他方式测试了它。 你能救我吗?
源代码:
package com.studiocrew.lk;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.content.*;
public class MainActivity extends Activity
{
EditText textIn;
Button buttonAdd;
Button buttonAddFirst;
LinearLayout container;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
buttonAddFirst = (Button)findViewById(R.id.addfirst);
buttonAddFirst.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonAdd.setVisibility(View.VISIBLE);
textIn.setVisibility(View.VISIBLE);
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.Settings:
Intent intent= new Intent(MainActivity.this,SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 0 :(得分:0)
我不知道保存TextView,但您当然可以在Bundle中保存TextView的内容以从onCreate()恢复。为Bundle添加一个全局字段以引用:
private TextView inputOut;
在buttonAdd.onClick()
中动态设置然后保存状态。
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.puString(INPUT_CONTENT, textOut.getText().toString());
super.onSaveInstanceState(savedInstanceState);
}
然后为onCreate()添加此检查。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String inputOut = savedInstanceState.getString(INPUT_CONTENT);
} else {
....
}
...
}
但如果你真的,真的想保存TextView对象......
我会对TextView类进行子类化并使其成为Parcelable / Serializable,以便可以将其添加到Bundle并从上面恢复。
例如:
public class MyTextView extends TextView implements Serializable {
private String inputText;
.....
....
...
}
并覆盖此:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerialiazable(MY_TEXTVIEW, textOut);
super.onSaveInstanceState(savedInstanceState);
}
老实说,不确定这是否有效,但理论上,是吗?
如果您尝试这样做,可以使用以下文章来帮助您选择Parcelable或Serializable:http://www.developerphil.com/parcelable-vs-serializable/
这也是为了保存状态: http://developer.android.com/training/basics/activity-lifecycle/recreating.html#RestoreState