正在使用带有此类复选框的alertdialog
CharSequence[] items = {"Google", "Apple", "Microsoft", "Google", "Apple", "Microsoft"};
boolean[] itemsChecked = new boolean[items.length];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(0);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
ContextThemeWrapper cw = new ContextThemeWrapper( this, R.style.AlertDialogTheme );
return new AlertDialog.Builder(cw/*this*/) //tried cw, but of no use
.setIcon(R.drawable.ic_launcher)
.setTitle("Dialog with simple text")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < items.length; i++) {
if (itemsChecked[i]) {
Toast.makeText(getBaseContext(), items[i] + " checked!", Toast.LENGTH_LONG).show();
}
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_LONG).show();
}
})
.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getBaseContext(), items[which] + (isChecked ? "checked!" : "unchecked!"), Toast.LENGTH_SHORT).show();
}
})
.create();
}
return null;
}
但宽度和高度完全超出我的控制范围,尝试改变主题,如
<style name="AlertDialogTheme" parent="android:Theme.Dialog">
<item name="android:textSize">14sp</item>
<item name="android:width">60dp</item>
<item name="android:height">100dp</item>
</style>
稍微改了一下,结果很糟糕,发现this很有意思,但我想我的情景不同(可能是,构建器变量超出我的范围,使用它)