Package Com.example.selectonlyonle;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.Dialog;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
public class Mainactivity extends Activity {
Private EditText EditText;
Private final static int DIALOG = 1;
Boolean[] flags = new boolean[] {false, False, False, false};//initial check case
string[] items = null;
Private String lv_items[] = {"Tire (400)", "no bone wiper (100)", "Brake pads (235)",
"Brake disc (500)"};
Private String lv_items_value[] = {"400", "100", "235", "500"};
Private String result;
private int motoal;
Private String test;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Items = Getresources (). Getstringarray (R.array.hobby);
EditText = (editText) Findviewbyid (R.id.edittext);
Button button = (button) Findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Show dialog box
ShowDialog (DIALOG);
}
});
}
/**
* Create check box dialog box
*/
@Override
Public Dialog oncreatedialog (int id) {
Dialog Dialog = null;
Switch (ID) {
Case DIALOG:
Alertdialog.builder Builder = new Alertdialog.builder (this);
Setting the icon for a dialog box
Builder.seticon (R.drawable.header);
Setting the caption of a dialog box
Builder.settitle ("check box dialog box");
Builder.setmultichoiceitems (R.array.hobby, Flags,
New Dialoginterface.onmultichoiceclicklistener () {
public void OnClick (Dialoginterface dialog, int which,
Boolean isChecked) {
Flags[which] = isChecked;
System.out.println (which+ "==which===" +which);
result = "You have selected:";
// for (int i = 0; i < flags.length; i++) {
if (Flags[which]) {
result = result + Items[which] + ",";
Motoal = Motoal
+ Integer
. ValueOf (Lv_items_value[which]);
SYSTEM.OUT.PRINTLN (Result + "======")
+ motoal);
Test = Test +lv_items_value[i];
}
// }
if (!flags[which]) {
Assuming it's selected and not selected.
Motoal = Motoal
-Integer
. ValueOf (Lv_items_value[which]);
System.out.println ("==motoal=0="
+ motoal);
}
for (int a = 0; a < 4; a++) {
if (flags[a] = = False) {
Motoal = Motoal
-Integer
. ValueOf (Lv_items_value[a]);
System.out.println ("==motoal=0="
+ motoal);
// }
// }
System.out.println ("=========" +test);
Edittext.settext (motoal+ "");
Edittext.settext (Motoal + "");
}
});
Builder.setpositivebutton (Result,
New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
// }
// });
Add a OK button
Builder.setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
}
});
Create a check box dialog box
Dialog = Builder.create ();
Break
}
return dialog;
}
}
----------------------Array.xml---------------------------------
<?xml version= "1.0" encoding= "Utf-8"?
>
<resources>
<string-array name= "Hobby" >
<item> tires (+) </item>
<item> non-Bone wiper blade (+) </item>
<item> Brake Pads (235) </item>
<item> Brake Discs ($) </item>
</string-array>
</resources>
This is looking at the online code, just a little bit of your own understanding. and the addition and subtraction of the data in the checkbox
------------------------------Activity_main.xml-----------
<?xml version= "1.0" encoding= "Utf-8"?
;
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
Android: Layout_width= "Fill_parent"
android:layout_height= "fill_parent"
Android:o rientation= "vertical"
<edittext
android:id= "@+id/ EditText "
android:layout_width=" fill_parent "
Android: layout_height= "Wrap_content"
android:cursorvisible= "false"
android:editable= "false"
android:text= ""/>
< Button
android:id= "@+id/button"
android:layout_width= " Fill_parent "
android:layout_height=" wrap_content "
android:text= "Show check box dialog box"/>
</linearlayout>
Android dialog box, checkbox, save data on the same page at the same time