這是我的問題。 如何在對話框中保持開關設置?
這是我的代碼:
dialog = new Dialog(FragmentOne.super.getActivity(),R.style.selectorDialog);
dialog.setContentView(R.layout.dialog_mod);
Switch switch1 = (Switch) dialog.findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
try {
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
} else {
try {
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}
});
dialog.show();
交換機2和交換機3是相同的代碼,交換機1,所以我通過這個部分。 當我再次打開時,它又被複位。 任何人都知道如何解決這個問題?
你必須拯救之開關的'狀態類似的地方'sharedPreferences'。然後從偏好中獲取值並相對於該值更新狀態。 – Wizard
使用switch1.setChecked(true); – Avi
檢查你的問題的答案 – Androider