正在使用类似这样的复选框的警报对话框
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>
稍微改了一下,结果很糟糕,发现这很有趣,但我想我的场景不同(可能是,builder变量超出了我的范围,无法使用它)