如果有人能帮我解决以下问题,我将永远感激不尽。
我的Android应用程序是一个进行财产调查的调查问卷。每项活动都涉及财产的一个要素,即厨房,浴室,中央供暖等。应用程序完成后,将会有大约50个问题。每个活动都有三个Spinners和两个Edit Texts,用户在进入下一个活动之前必须使用该文本输入与属性相关元素的年龄和条件相关的数据。
我的问题如下:
问题1涉及厨房。一旦输入了所有相关数据,我就使用意图(通过“下一页”按钮)开始与浴室相关的下一个活动。但是,如果我意识到我在厨房活动中输入了数据并通过浴室活动的意图返回(以同样的方式从厨房活动进入浴室活动),我之前输入的数据是否那里更长如何保留这些数据?我的应用程序用户必须能够在调查问题之间向后和向前翻转并查看他们之前输入的数据。一旦回答了所有50个问题,数据将被保存到数据库中,然后可以调查下一个属性。
我已经在互联网和各种书籍上搜索了答案,但我遇到了相互矛盾的信息。有人说在Pause使用,有些人在Stop上说,其他人在Saved Instance State上说。我糊涂了??我已经被困在这三天了,所以非常感谢任何帮助。
下面的厨房活动,然后是浴室活动..........
package com.example.basicview6;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class Kitchen扩展了Activity {
// defining the variables that will be displayed on the page
String[] age, renewal, main;
Spinner s1, s2, sMain;
ToggleButton repairs;
EditText repDesc, repCost, quantity;
Button back, next;
TextView life, qty, unit;
// creating the layout from the main xml file
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kitchen);
// getting the string array values from the 'strings xml' resources file
// and applying them to the relevant variable
age = getResources().getStringArray(R.array.age_array);
renewal = getResources().getStringArray(R.array.renewal_array);
main = getResources().getStringArray(R.array.kitchen_array);
// getting the Spinner widget from the main xml and applying it to the
// 's1' variable
s1 = (Spinner) findViewById(R.id.spAge);
s2 = (Spinner) findViewById(R.id.spRenewal);
sMain = (Spinner) findViewById(R.id.spKitchen);
repairs = (ToggleButton) findViewById(R.id.tbRepairs);
repDesc = (EditText) findViewById(R.id.etRepDesc);
repCost = (EditText) findViewById(R.id.etRepCost);
quantity = (EditText) findViewById(R.id.etQuantity);
back = (Button) findViewById(R.id.bBack);
next = (Button) findViewById(R.id.bNext);
life = (TextView) findViewById(R.id.tvLife);
qty = (TextView) findViewById(R.id.tvQuantity);
unit = (TextView) findViewById(R.id.tvUnitM);
life.setVisibility(View.INVISIBLE);
quantity.setVisibility(View.INVISIBLE);
qty.setVisibility(View.INVISIBLE);
unit.setVisibility(View.INVISIBLE);
/*
* creating a new 'string type' ArrayAdapter and telling it to display
* the values of the relevant variable as a simple spinner item
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, age);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, renewal);
ArrayAdapter<String> adapterM = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, main);
// priming the s1 Spinner variable for an array item to be selected -
// standby mode
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
s2.setAdapter(adapter2);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sMain.setAdapter(adapterM);
sMain.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
// setting up the OnClickListerner for the repairs toggle button
repairs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (repairs.isChecked()) {
repDesc.setVisibility(View.VISIBLE);
repCost.setVisibility(View.VISIBLE);
} else {
repDesc.setVisibility(View.INVISIBLE);
repCost.setVisibility(View.INVISIBLE);
}
}
});
// setting up the OnClickListener for the Next button
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openBathroom = new Intent(
"com.example.basicview6.BATHROOM");
startActivity(openBathroom);
}
});
}
}
浴室活动...................
package com.example.basicview6;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
公共类浴室扩展活动{
// defining the variables that will be displayed on the page
String[] age, renewal, main;
Spinner s1, s2, sMain;
ToggleButton repairs;
EditText repDesc, repCost, quantity;
Button back, next;
TextView life, qty, unit;
// creating the layout from the main xml file
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bathroom);
// getting the string array values from the 'strings xml' resources file
// and applying them to the relevant variable
age = getResources().getStringArray(R.array.age_array);
renewal = getResources().getStringArray(R.array.renewal_array);
main = getResources().getStringArray(R.array.bathroom_array);
// getting the Spinner widget from the main xml and applying it to the
// 's1' variable
s1 = (Spinner) findViewById(R.id.spAge);
s2 = (Spinner) findViewById(R.id.spRenewal);
sMain = (Spinner) findViewById(R.id.spBathroom);
repairs = (ToggleButton) findViewById(R.id.tbRepairs);
repDesc = (EditText) findViewById(R.id.etRepDesc);
repCost = (EditText) findViewById(R.id.etRepCost);
quantity = (EditText) findViewById(R.id.etQuantity);
back = (Button) findViewById(R.id.bBack);
next = (Button) findViewById(R.id.bNext);
life = (TextView) findViewById(R.id.tvLife);
qty = (TextView) findViewById(R.id.tvQuantity);
unit = (TextView) findViewById(R.id.tvUnitM);
life.setVisibility(View.INVISIBLE);
quantity.setVisibility(View.INVISIBLE);
qty.setVisibility(View.INVISIBLE);
unit.setVisibility(View.INVISIBLE);
/*
* creating a new 'string type' ArrayAdapter and telling it to display
* the values of the relevant variable as a simple spinner item
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, age);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, renewal);
ArrayAdapter<String> adapterM = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, main);
// priming the s1 Spinner variable for an array item to be selected -
// standby mode
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
s2.setAdapter(adapter2);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sMain.setAdapter(adapterM);
sMain.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
// setting up the OnClickListerner for the repairs toggle button
repairs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (repairs.isChecked()) {
repDesc.setVisibility(View.VISIBLE);
repCost.setVisibility(View.VISIBLE);
} else {
repDesc.setVisibility(View.INVISIBLE);
repCost.setVisibility(View.INVISIBLE);
}
}
});
// setting up the OnClickListener for the Back button
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openKitchen = new Intent(
"com.example.basicview6.KITCHEN");
startActivity(openKitchen);
}
});
}
}
答案 0 :(得分:0)
那么你可以使用许多不同的方法,这就是为什么你一直看到相互矛盾的答案。然而,有一些是首选。
对于屏幕旋转,意味着如果设备水平或垂直翻转,则使用onSaveInstanceState
。
还有另一种我认为是作弊的方法,许多人会告诉你远离它,因为它可能导致很多错误。然而,这是最简单的方法。只需进入您的清单并放置此行
即可android:configChanges="orientation|keyboardHidden|screenSize
如果您尝试保留信息,那么当您返回Activity
时,它仍然存在,您应该使用onPause()
。您似乎只传输了String
,int
等简单数据,这可以使用SharedPreferences
内的onPause()
来完成。原因是onPause()
始终(99%的时间)在Activity
被杀之前被调用,这意味着您将始终保留您的信息。
有些视频参考会向您展示如何执行这些操作,请转到此处The New Boston
如果你想在这里阅读一些链接
如果您还需要其他任何东西,请询问。
答案 1 :(得分:0)
在活动之间切换时,您可以将额外内容添加到意图:putExtra(),如:
intent.putExtra(Intent.EXTRA_TEXT, "my saved String");
通过以下方式让他们进入被叫活动:
String gottenString = intent.getExtras().getString(Intent.EXTRA_TEXT);
您还可以将数据保存在(临时)文件中:Saving Files