基本
private void myAlertDialog() {
Builder MyAlertDialog = new AlertDialog.Builder(this);
MyAlertDialog.setTitle("title");
MyAlertDialog.setMessage("info");
DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 如果不做任何事情 就會直接關閉 對話方塊
}
};
;
MyAlertDialog.setNeutralButton("center", OkClick);
MyAlertDialog.setPositiveButton("left", OkClick);
MyAlertDialog.setNegativeButton("right", OkClick);
MyAlertDialog.show();
}
List
private void myListAlertDialog() {
final String[] ListStr = { "1", "2", "3", "4", "5" };
Builder MyListAlertDialog = new AlertDialog.Builder(this);
MyListAlertDialog.setTitle("title");
// 建立List的事件
DialogInterface.OnClickListener ListClick = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Basis_AlertDialogActivity.this, ListStr[which],// 顯示所點選的選項
Toast.LENGTH_LONG).show();
}
};
// 建立按下取消什麼事情都不做的事件
DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
};
MyListAlertDialog.setItems(ListStr, ListClick);
MyListAlertDialog.setNeutralButton("cancel", OkClick);
MyListAlertDialog.show();
}
自定義
AlertDialog builder;
private void showDialogLayout(Context context) {
LayoutInflater inflater = LayoutInflater.from(this);
final View textEntryView = inflater
.inflate(R.layout.dialoglayout, null);
builder = new AlertDialog.Builder(context).create();
Button OKBut = (Button) textEntryView.findViewById(R.id.okbut);
OKBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Basis_AlertDialogActivity.this, "DialogBtn",
Toast.LENGTH_LONG).show();
}
});
Button BackBut = (Button) textEntryView.findViewById(R.id.backbut);
BackBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
builder.cancel();
}
});
builder.setView(textEntryView);
builder.show();
}
請先 登入 以發表留言。