我试图建立一个配置页面,允许用户选择是否要记住他的密码。 但我不知道在哪里做"设置"并且"得到"共享首选项以及发送意图的位置,因为其他类不是活动。
这是我的片段:
public class configFragment extends android.support.v4.app.Fragment {
CheckBox check;
public static Boolean checkBoxValue;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.config_layout, container, false);
TextView titre=(TextView) rootView.findViewById(R.id.configu);
check=(CheckBox) rootView.findViewById(R.id.checkbox_meat);
TextView avertissement=(TextView) rootView.findViewById(R.id.avertissement);
titre.setText("Configuration");
avertissement.setText("Attention ! : Le code n'est pas chiffré!");
check();
return rootView;
}
public boolean check(){
if(check.isChecked()){
Intent intent=new Intent(getActivity(),HomeActivity.class);
intent.putExtra("value",true);
return true;
}else{
return false;
}
}
}
这是另一个类中的方法:
public void askForPassword() {
final EditText et = new EditText(mHomeViewModel.getFragment().getActivity());
et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
SharedPreferences loginPreferences = mHomeViewModel.getFragment().getActivity().getSharedPreferences(SPF_NAME,
Context.MODE_PRIVATE);
et.setText(loginPreferences.getString("password", ""));
new AlertDialog.Builder(mHomeViewModel.getFragment().getActivity())
.setTitle("Saisissez votre code Le Coffre")
.setView(et)
.setPositiveButton("Valider", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* if(value==true){
SharedPreferences settings = mHomeViewModel.getFragment().getActivity().getSharedPreferences(PREFRENCES_NAME, 0);
settings.edit().putString("pwd", et.getText().toString()).commit();
// BsApplication.sPassword = password;
}else {
BsApplication.sPassword = et.getText().toString();
//}
}
initBsAndCloudAndBrowse();
}*/
BsApplication.sPassword = et.getText().toString();
initBsAndCloudAndBrowse();}
})
.setNegativeButton("Première connexion?", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
final EditText et1 = new EditText(mHomeViewModel.getFragment().getActivity());
et1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
new AlertDialog.Builder(mHomeViewModel.getFragment().getActivity())
.setView(et1)
.setTitle("Première connexion")
.setMessage("Choisissez un code différent de celui du cloud Orange "+"\n"+"Attention nous ne connaitrons pas votre code et n’offrons pas encore la possibilité de le changer ")
.setPositiveButton("Créer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
BsApplication.sPassword = et1.getText().toString();
initBsAndCloudAndBrowse();
}
})
.show();
}
})
.show();
}
感谢您的帮助,我真的很感激
答案 0 :(得分:0)
由于您正在使用键/值创建用于打开HomeActivity的Intent,因此您应该能够在onCreate上检索该值:
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = savedInstanceState != null ? savedInstanceState : getArguments();
boolean foo;
if (extras != null) {
foo = extras.get("value");
}
}
但是,由于您要求在SharedPreference中存储此值,因此此链接可能会更有帮助https://developer.android.com/training/basics/data-storage/shared-preferences.html