android dialog box, checkbox, save data on same page at the same time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.