评论

收藏

[其他] Android alertDialog

网络安全 网络安全 发布于:2021-08-04 15:34 | 阅读数:318 | 评论:0

package com.example.dialogue;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] { true, true, false,
false };

Dialog alertDialog = new AlertDialog.Builder(this)
.setTitle("你喜欢吃哪种水果?")
.setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected,
new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
})
.setPositiveButton("确认", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true) {
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(MainActivity.this,
stringBuilder.toString(), Toast.LENGTH_SHORT)
.show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).create();
alertDialog.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


关注下面的标签,发现更多相似文章