开发者

Using Theme Dialog Layout and XML Strings

I have a layout, main.xml. When you click the button describeButton, it opens a new theme.dialog layout called checklist.xml. On checklist.xml, users are given a series of check boxes they can use to describe a picture. After they check as many check boxes as they please, they can click the okButton to go back to the main.xml layout.

So here are my questions:

  • How do I take the check boxes that users selected, turn the selections into a string, and store it as a stri开发者_运维百科ng in the strings.xml file?

  • How do I code the okButton on checklist.xml to close the layout, which in turn will return the users to main.xml?

Thank you for your help.

Edit

Here's the code I'm using at the moment:

public class Descriptor extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checklist);

        final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);



        Button selOK = (Button) findViewById(R.id.selectOK);
        selOK.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 if (checkBox1.isChecked()) {
                     // add a string to an existing string in strings.xml
                 }
                 // repeat this if statement for all checkboxes

                 // execute something to close this layout
            }
        });
    }
}


Are you talking about res/values/strings.xml? After you compile an APK you cannot change the XML files. During compilation the XML files are used to generate an R.java file which is a java implementation of your XML files. These files are actual java source that you can't change or alter.

One way to achieve what I think you are trying to do is to attach a listener to the checkboxes and then keep track of when a user checks or un-checks a box. Here is an example of a dialog with radio buttons from the Android Docs for Dialogs. It should be pretty simple to convert to checkboxes by using builder.setMultiChoiceItems instead of builder.setSingleChoiceItems. In fact here is a link to the multiChoiceItems method doc.

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();

With regards to your second question, your OkButton can just close the dialog. In your main activity can setup an array of booleans with one boolean for every string you are going to display. Then in your the onClick method can change the individual boolean values based upon which checkboxes are checked. Then when the dialog closes you can examine the array of booleans to see what checkboxes the users checked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜