Tuesday, 20 August 2013

How to add functions a checkbox in Android

How to add functions a checkbox in Android

I want to make two checkbox in an alertdialog of my Application. This is
what I want exactly: One is a "Don't ask me again" and the other is a
"Delete Cache", and two buttons, Cancel and Exit. What I want to do is
simple but no than simple. I think taht you understand what I want only
with read it. I need Your help!
THIS IS MY CODE:
public class AlertDialog extends Activity {
public static final String PREFS_NAME = "MyPrefsFile1";
public CheckBox dontShowAgain;
public CheckBox dontShowAgain2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** Called when the activity is brought to front. */
@Override
protected void onResume() {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
LayoutInflater adbInflater = LayoutInflater.from(this);
View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip2);
adb.setView(eulaLayout);
adb.setTitle("Attention");
adb.setMessage(Html.fromHtml("Do you want to do it?"));
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String checkBoxResult = "NOT checked";
if (dontShowAgain.isChecked())
checkBoxResult = "checked";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("skipMessage", checkBoxResult);
// Commit the edits!
editor.commit();
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://misha.beshkin.lv/android-alertdialog-with-checkbox/")));
return;
}

No comments:

Post a Comment