简体   繁体   English

如何在没有onConfigChanged的情况下处理android中的屏幕方向更改

[英]How to handle Screen Orientation change in android without onConfigChanged

I am new to android coding, So i'm sure there must be some stupid mistake in my code. 我是android编码的新手,所以我确定我的代码中肯定有一些愚蠢的错误。 I am trying to make a TO DO LIST app (without using Listview) and I don't know how to handle orientation change in Android. 我正在尝试制作一个“待办事项列表”应用程序(不使用Listview),但我不知道如何处理Android中的方向更改。 When i rotate the device all my data is lost. 当我旋转设备时,我所有的数据都会丢失。 If you can suggest how I can go about it, I would be really grateful. 如果您能提出建议,我将不胜感激。

Here is my source code: 这是我的源代码:

MainActivity.java 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 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 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" />

Save the state of the activity you would like to reuse later in savedInstanceState. 将您想在以后重用的活动状态保存在saveInstanceState中。 And then you check if the savedInstanceState == null in onCreate. 然后检查onCreate中的savedInstanceState == null。 If it is null then you create the activity if not you get the state. 如果为空,则创建活动,否则将获得状态。 This will show you how to do it with examples. 将通过示例向您展示如何做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM