使用主题对话框布局和 XML 字符串
我有一个布局,main.xml
。当您单击按钮 describeButton
时,它会打开一个名为 checklist.xml
的新 theme.dialog 布局。在 checklist.xml
上,为用户提供了一系列可用于描述图片的复选框。在选中任意数量的复选框后,他们可以单击 okButton 返回到 main.xml 布局。
所以我的问题是:
如何获取用户选择的复选框,将选择转换为字符串,并将其作为字符串存储在
strings.xml
文件中?如何在
checklist.xml
上编写okButton
来关闭布局,从而将用户返回到main.xml
?
感谢您的帮助。
编辑
这是我目前使用的代码:
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
}
});
}
}
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 string in the
strings.xml
file?How do I code the
okButton
onchecklist.xml
to close the layout, which in turn will return the users tomain.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
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是在谈论
res/values/strings.xml
吗?编译 APK 后,您无法更改 XML 文件。在编译期间,XML 文件用于生成 R.java 文件,该文件是 XML 文件的 java 实现。这些文件是实际的 java 源代码,您无法更改或改变。实现我认为您想要做的事情的一种方法是将侦听器附加到复选框,然后跟踪用户何时选中或取消选中某个框。以下是来自 Android 对话框文档。使用
builder.setMultiChoiceItems
而不是builder.setSingleChoiceItems
转换为复选框应该非常简单。事实上,这里是一个指向 multiChoiceItems 方法文档的链接。关于你的第二个问题,你的 OkButton 可以关闭对话框。在您的主要活动中,可以设置一组布尔值,其中每个要显示的字符串都有一个布尔值。然后,在您的
onClick
方法中,可以根据选中的复选框更改各个布尔值。然后,当对话框关闭时,您可以检查布尔数组以查看用户选中了哪些复选框。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 ofbuilder.setSingleChoiceItems
. In fact here is a link to the multiChoiceItems method doc.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.