我有两项活动: 1.包含listview的主要活动; 2.在主要活动中将项目添加到列表视图的第二个活动。
对于第二个活动,我为景观创建了布局 - 土地布局。
在第二个活动以纵向方式打开后,我将其更改为横向模式 - 第二个活动关闭,应用程序返回主要活动。
问题: 1.方向更改后如何保存输入到EditText字段值?
2.如何在将屏幕方向更改为横向时将layout-land应用于Second Activity?
UPD 第二个活动代码:
public class AddItem extends MainScreen implements OnClickListener{
final String LOG_TAG = "myLogs";
EditText comment_enter, link_enter, password_enter, login_enter, title_enter, date_enter;
Button add_item_button, add_more_button, clear_close_button;
CheckBox showPass;
DBHelper db;
DataBase DB;
SimpleCursorAdapter passListViewAdapter;
SimpleDateFormat sdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);
Log.d(LOG_TAG, "before edit : ");
comment_enter = (EditText) findViewById(R.id.comment_enter);
link_enter = (EditText) findViewById(R.id.link_enter);
password_enter = (EditText) findViewById(R.id.password_enter);
login_enter = (EditText) findViewById(R.id.login_enter);
title_enter = (EditText) findViewById(R.id.title_enter);
date_enter = (EditText) findViewById(R.id.date_enter);
showPass = (CheckBox) findViewById(R.id.showPass);
showPass.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.d(LOG_TAG, "is checked : " + isChecked);
if (isChecked) {
password_enter.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}
else {
password_enter.setInputType(129);
}
}
});
add_item_button = (Button) findViewById(R.id.add_item_button);
add_item_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d(LOG_TAG, "add_item_button : ");
String title_str = title_enter.getText().toString();
String login_str = login_enter.getText().toString();
String pass_str = password_enter.getText().toString();
String link_str = link_enter.getText().toString();
String comm_str = comment_enter.getText().toString();
String date_str = date_enter.getText().toString();
byte[] login_byted = null;
try {
login_byted = login_str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String login_crypted = Base64.encodeToString(login_byted, Base64.DEFAULT);
Log.d(LOG_TAG, "Crypted login" + login_crypted);
byte[] pass_byted = null;
try {
pass_byted = pass_str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String pass_crypted = Base64.encodeToString(pass_byted, Base64.DEFAULT);
Log.d(LOG_TAG, "Crypted login" + pass_crypted);
DataBase DB = new DataBase(AddItem.this);
DB.open();
DB.insertPass(title_str, login_crypted, pass_crypted, link_str, comm_str, date_str);
DB.close();
Log.d(LOG_TAG, "after inserting into DB : ");
finish();
}
});
add_more_button = (Button) findViewById(R.id.add_more_button);
add_more_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d(LOG_TAG, "add_more_button : ");
String title_str = title_enter.getText().toString();
String login_str = login_enter.getText().toString();
String pass_str = password_enter.getText().toString();
String link_str = link_enter.getText().toString();
String comm_str = comment_enter.getText().toString();
String date_str = date_enter.getText().toString();
byte[] login_byted = null;
try {
login_byted = login_str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String login_crypted = Base64.encodeToString(login_byted, Base64.DEFAULT);
Log.d(LOG_TAG, "Crypted login" + login_crypted);
byte[] pass_byted = null;
try {
pass_byted = pass_str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String pass_crypted = Base64.encodeToString(pass_byted, Base64.DEFAULT);
Log.d(LOG_TAG, "Crypted login" + pass_crypted);
DataBase DB = new DataBase(AddItem.this);
DB.open();
DB.insertPass(title_str, login_crypted, pass_crypted, link_str, comm_str, date_str);
DB.close();
Log.d(LOG_TAG, "after inserting into DB : ");
fieldClear();
String link_enter_str = link_enter.getText().toString();
if(link_enter_str.equals("")){
link_enter.setText("http://www.");
}
}
});
clear_close_button = (Button) findViewById(R.id.clear_close_button);
clear_close_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d(LOG_TAG, "clear/close click button : ");
boolean checkRes = emptyAllCheck();
Log.d(LOG_TAG, "result : " + checkRes);
if(checkRes == true){
finish();
}
fieldClear();
}
});
if(savedInstanceState != null){
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.d(LOG_TAG, "title : " + savedInstanceState.getString("title"));
title_enter.setText(savedInstanceState.getString("title"));
login_enter.setText(savedInstanceState.getString("login"));
password_enter.setText(savedInstanceState.getString("pass"));
link_enter.setText(savedInstanceState.getString("link"));
comment_enter.setText(savedInstanceState.getString("comm"));
date_enter.setText(savedInstanceState.getString("date"));
add_item_button = (Button) findViewById(R.id.add_item_button);
add_more_button = (Button) findViewById(R.id.add_more_button);
}
else {
Log.d(LOG_TAG, "before getting date : ");
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String date = sdf.format(new Date(System.currentTimeMillis()));
date_enter.setText(date);
}
}
@Override
protected void onPause() {
super.onPause();
finish();
Log.d(LOG_TAG, "onPause : ");
}
@Override
protected void onResume(){
super.onResume();
}
protected void onSaveInstanceState(Bundle saveInstance) {
super.onSaveInstanceState(saveInstance);
String title_str = title_enter.getText().toString();
String login_str = login_enter.getText().toString();
String pass_str = password_enter.getText().toString();
String link_str = link_enter.getText().toString();
String comm_str = comment_enter.getText().toString();
String date_str = date_enter.getText().toString();
saveInstance.putString("title", title_str);
saveInstance.putString("login", login_str);
saveInstance.putString("pass", pass_str);
saveInstance.putString("link", link_str);
saveInstance.putString("comm", comm_str);
saveInstance.putString("date", date_str);
Log.d(LOG_TAG, "onSaveInstanceState +" + title_str + login_str + pass_str + link_str + comm_str + date_str);
}
public void fieldClear(){
comment_enter.setText("");
link_enter.setText("http://www.");
password_enter.setText("");
login_enter.setText("");
title_enter.setText("");
}
public boolean emptyAllCheck(){
String title_str = title_enter.getText().toString();
String login_str = login_enter.getText().toString();
String pass_str = password_enter.getText().toString();
String link_str = link_enter.getText().toString();
String comm_str = comment_enter.getText().toString();
if (title_str.equals("") && login_str.equals("") && pass_str.equals("") && link_str.equals("http://www.") && comm_str.equals("")) {
return true;
}
return false;
}
}
答案 0 :(得分:3)
我建议你多阅读一下Android Activity生命周期,它会对你有所帮助。 但是在配置上改变android破坏你是活动并重新创建,你可以使用回调方法OnsavedInstanceState()来保存你的实例(它将由系统在配置更改时自动调用)
示例
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);{
String savedText = myEditText.getText().toString();
savedInstanceState.putString("Key", savedText);
}
现在,在OnCreate方法上重新创建应用程序时,请检索保存的文本,如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null){
myEditText.setText(savedInstanceState.getString("Key");
//the rest of the code}
Voila和我希望那就是你的意思。
答案 1 :(得分:-1)
使用这个:
android:configChanges="orientation|screenSize"
在清单文件中添加此代码。无需保存编辑文本变量。