我有一个布局main.xml
。当您单击按钮describeButton
时,它会打开一个名为checklist.xml
的新theme.dialog布局。在checklist.xml
上,用户可以使用一系列复选框来描述图片。在他们检查了多个复选框后,他们可以点击okButton
返回main.xml
布局。
所以这是我的问题:
如何选中用户选中的复选框,将选择项转换为字符串,并将其作为字符串存储在strings.xml
文件中?
如何对okButton
上的checklist.xml
进行编码以关闭布局,从而将用户返回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
}
});
}
}
答案 0 :(得分:1)
你在谈论res/values/strings.xml
吗?编译APK后,您无法更改XML文件。在编译期间,XML文件用于生成R.java文件,该文件是XML文件的java实现。这些文件是您无法更改或更改的实际Java源代码。
实现我认为你要做的事情的一种方法是将一个监听器附加到复选框,然后跟踪用户何时检查或取消选中一个框。以下是Android Docs for Dialogs中带有单选按钮的对话框示例。使用builder.setMultiChoiceItems
代替builder.setSingleChoiceItems
转换为复选框应该非常简单。实际上这是一个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();
关于你的第二个问题,你的OkButton可以关闭对话框。在您的主要活动中,可以为要显示的每个字符串设置一个布尔值数组,其中包含一个布尔值。然后在onClick
方法中,可以根据选中的复选框更改各个布尔值。然后当对话框关闭时,您可以检查布尔数组以查看用户选中的复选框。