앱이 시작될 때 시작되는 AlertDialog에 확인란이 있습니다. 확인란의 선택 여부를 저장하기 위해 "SharedPreferences"를 추가했습니다. 확인란을 선택하면 AlertDialog가 시작되지 않거나 앱이 시작될 때 표시됩니다. 하지만 내 문제는이 확인란을 선택하면 앱을 시작할 때마다 AlertDialog가 시작됩니다.
checkbox.xml (레이아웃 파일) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/checkboxDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
style="?android:attr/textAppearanceMedium" />
</LinearLayout>
에서 onCreate 방법 에서 MainActivity.java :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View checkBoxView = View.inflate(this, R.layout.checkbox, null);
final CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkboxDialog);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences prefs = getSharedPreferences("state", 0);
if(prefs.getBoolean("x", false) == true)
{
checkBox.setChecked(true);
}
else if(checkBox.isChecked() == true)
{
SharedPreferences myPrefs = getSharedPreferences("state", 0);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putBoolean("x", true);
editor.commit();
}
}
});
checkBox.setText("Test");
final Context context = this;
AlertDialog.Builder mainDialog = new AlertDialog.Builder(context);
mainDialog.setTitle("Test");
mainDialog.setMessage("Test my dialog!");
mainDialog.setView(checkBoxView);
mainDialog.setPositiveButton("Go to website", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent browseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
startActivity(browseIntent);
}
});
AlertDialog alertDialog = mainDialog.create();
alertDialog.show();
...
}
여기에서 답을 찾았습니다.
다른 사람들에게 도움이되기를 바랍니다.
감사.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다