[英]How to handle Screen Orientation change in android without onConfigChanged
我是android编码的新手,所以我确定我的代码中肯定有一些愚蠢的错误。 我正在尝试制作一个“待办事项列表”应用程序(不使用Listview),但我不知道如何处理Android中的方向更改。 当我旋转设备时,我所有的数据都会丢失。 如果您能提出建议,我将不胜感激。
这是我的源代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout mlistlayout;
String toDo;
private ArrayList<Entry> mlist;
static class Entry {
String S;
boolean b;
public Entry(String S, boolean b) {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlistlayout = (LinearLayout) findViewById(R.id.list);
mlist = (ArrayList<Entry>) getLastCustomNonConfigurationInstance();
if (mlist == null) {
mlist = new ArrayList<>();
}
Button sub = (Button) findViewById(R.id.buttonsubmit);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText1 = (EditText) `
findViewById(R.id.editText1);
toDo = editText1.getText().toString().trim();
if (toDo.isEmpty()) {
return;
}
editText1.setText("");
AddListEntry(toDo);
}
});
}
@Override
public Object onRetainCustomNonConfigurationInstance() {
return mlist;
}
void AddListEntry(String S) {
LayoutInflater inflater = LayoutInflater.from(this);
View v = inflater.inflate(R.layout.row, mlistlayout, false);
final Entry entry = new Entry(S, false);
TextView t = (TextView) v.findViewById(R.id.task_title);
t.setText(toDo);
CheckBox cb=(CheckBox) v.findViewById(R.id.task_delete);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
entry.b=isChecked;
}
});
mlist.add(entry);
mlistlayout.addView(v);
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bag"
tools:context="calpoly.edu.todolist.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:inputType="text"
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_weight="2" />
<Button
android:id="@+id/buttonsubmit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/submit" />
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/list"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"></LinearLayout>
</ScrollView>
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linear_layout"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="20sp" />
将您想在以后重用的活动状态保存在saveInstanceState中。 然后检查onCreate中的savedInstanceState == null。 如果为空,则创建活动,否则将获得状态。 这将通过示例向您展示如何做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.