我有一个片段:
ContactUsFragment.java
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
/**
*
*/
public class ContactUsFragment extends Fragment {
private EditText et_name, et_phoneno;
private String name = "", phone = "";
private boolean instanceSaved = false;
public ContactUsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
Toast.makeText(getActivity().getBaseContext(), "Not null", Toast.LENGTH_LONG).show();
name = savedInstanceState.getString("sv_name");
phone = savedInstanceState.getString("sv_phone");
instanceSaved = savedInstanceState.getBoolean("InstanceSaved");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_contact_us, container, false);
registerETs(view);
if(instanceSaved) {
Toast.makeText(getActivity().getBaseContext(), "Instance was saved", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity().getBaseContext(), name, Toast.LENGTH_LONG).show(); //--This line works. Gives the correct value of string upon orientation change--
Toast.makeText(getActivity().getBaseContext(), phone, Toast.LENGTH_LONG).show();
et_name.setText(name); //--This line does not work--
et_name.setText("Another String"); //--This line does not work--
et_phoneno.setText(phone); //--This line does not work--
}
et_name.setText("A String"); //--This line works--
return view;
}
@Override
public void onStart() {
super.onStart();
}
public void registerETs(View view) {
et_name = (EditText)view.findViewById(R.id.input_name);
et_phoneno = (EditText)view.findViewById(R.id.input_phoneno);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Toast.makeText(getActivity().getBaseContext(), "saving", Toast.LENGTH_LONG).show();
savedInstanceState.putString("sv_name", et_name.getText().toString());
savedInstanceState.putString("sv_phone", et_phoneno.getText().toString());
savedInstanceState.putBoolean("InstanceSaved", true);
}
}
我面临的问题是,et_name.setText(name)
内部if(instanceSaved){...}
无法正常工作。它什么也没设置。好像, name 正在null
从if(savedInstanceSaved != null){...}
. 但是Toasts
我在各种代码块中使用的代码按预期工作。它正在保存onSaveInstanceState(Bundle savedInstanceState){...}
,正在执行if(savedInstanceSaved != null){...}
,这意味着 mysavedInstanceState
不是 null 并且还显示了name
string in的正确值if(instanceSaved){...}
。这意味着我的代码在更改期间可以正常工作orientation
。但它没有在setText(String)
方法中设置任何文本。
我也尝试过使用setText("String")
,并且有效。如代码所示。我做错了什么,我错过了什么?请帮忙。谢谢你。
注意:我在
EditText
里面使用 sandroid.support.design.widget.TextInputLayout
编辑:我发现无论如何,
setText(name)
总是取String name
. 尽管name
inToast
显示了新的更新值,但setText()
总是以某种方式始终使用初始值。另外,我注意到如果我三次更改方向,应用程序就会崩溃。