0

我创建了两个片段。第二个片段包含多个edittext。 SharedPreference运行良好,但只在最后一个编辑文本上。对于剩下的,它不保存任何东西。最后,当我们在edittext中编写,然后再次保存并运行时,应用程序仍会显示以前保存的日期。Sharedpreferences片段

EditText et; 

public TwoFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_two, container, false); 

    et =(EditText) view.findViewById(R.id.strength_score); 
    et =(EditText) view.findViewById(R.id.strength_modif); 
    et =(EditText) view.findViewById(R.id.strength_tem_scor); 
    et =(EditText) view.findViewById(R.id.strength_tem_modi); 


    SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", Context.MODE_PRIVATE); 
    et.setText(setting.getString("value", "")); 




    // Inflate the layout for this fragment 
    return view; 
} 

public void onStop(){ 
    super.onStop(); 
    if(et.getText() != null) { 
     SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", 0); 
     SharedPreferences.Editor editor = setting.edit(); 
     editor.putString("value", et.getText().toString()); 
     editor.commit(); 
    } 
    } 
} 

感谢您的帮助。

+0

你有4个edittexts具有相同的名称,最终,你会改变只有最后一个文本..? – DAVIDBALAS1

+0

您需要为不同的EditText使用不同的对象。 – NarendraJi

+0

您是否尝试过使用EventBus在两个片段之间或活动和片段之间进行通信? – DJO

回答

1

做这样的 -

EditText et,et1,et2,et3; 

public TwoFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_two, container, false); 

    et =(EditText) view.findViewById(R.id.strength_score); 
    et1 =(EditText) view.findViewById(R.id.strength_modif); 
    et2 =(EditText) view.findViewById(R.id.strength_tem_scor); 
    et3 =(EditText) view.findViewById(R.id.strength_tem_modi); 


    SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", Context.MODE_PRIVATE); 
    et.setText(setting.getString("value", "")); 
    et1.setText(setting.getString("value1", "")); 
    et2.setText(setting.getString("value2", "")); 
    et3.setText(setting.getString("value3", "")); 




    // Inflate the layout for this fragment 
    return view; 
} 

public void onStop(){ 
    super.onStop(); 
    if(et.getText() != null) { 
     SharedPreferences setting = this.getActivity().getSharedPreferences("PRESS", 0); 
     SharedPreferences.Editor editor = setting.edit(); 
     editor.putString("value", et.getText().toString()); 
     editor.putString("value1", et1.getText().toString()); 
     editor.putString("value2", et2.getText().toString()); 
     editor.putString("value3", et3.getText().toString()); 
     editor.commit(); 
    } 
    } 
} 
+0

非常感谢。 –

+0

@LuigiDeSimone请同意,如果它工作:) – NarendraJi