Android中AlertDialog的簡單使用
阿新 • • 發佈:2018-11-07
android中彈框很多種,alert框,dialog,AlertDialog,popupwindow,DialogFragment,toast,dialog樣式的activity等等,今天講解AlertDialog的簡單使用。
1 AlertDialog.Builder 設定相關引數
圖示,title,message,按鈕,是否點選消失等等。
public void showDialog(){
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource (R.mipmap.ic_launcher);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//title上的圖示和文字
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("發現新版本");
//自定義的title
//builder.setCustomTitle(imageView);
//dialog中間顯示的資訊
// builder.setMessage("Message");
//設定是否可以取消
builder.setCancelable(false);
//Set a custom view resource to be the contents of the Dialog
//不是全部view,只是message下的contents
//builder.setView(R.layout.activity_main);
builder.setItems(new String[]{"aaaa", "bbbbb", "cccc"}, new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//多選,單選item
// builder.setMultiChoiceItems();
// builder.setSingleChoiceItems();
//確認按鈕
builder.setPositiveButton(
"立即更新",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//取消按鈕
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//具體位置和程式碼載入順序無關,跟樣式有關
builder.setNeutralButton("其他",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//title上的圖示和文字
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("發現新版本");
//dialog中間顯示的資訊
builder.setMessage("Message");
//設定是否可以取消
builder.setCancelable(false);
//Set a custom view resource to be the contents of the Dialog
//不是全部view,只是message下的contents
builder.setView(R.layout.activity_main);
//確認按鈕
builder.setPositiveButton(
"立即更新",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//取消按鈕
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
如果只想顯示按鈕,不錯任何操作:
builder.setNegativeButton(“取消”,null);
//點選外部是否消失
setCanceledOnTouchOutside( false );
2 dialog 設定(字型大小,顏色設定)
dialog.show之後,依然可以進行dialog的設定。
AlertDialog dialog = builder.create();
dialog.show();
// 必須在執行show之後才能來設定
TextView tvMsg = (TextView) dialog.findViewById(android.R.id.message);
tvMsg.setTextSize(16);
tvMsg.setTextColor(Color.RED);
//設定按鈕的字型,顏色
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextSize(17);
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(Color.BLUE);
dialog.getButton(dialog.BUTTON_POSITIVE).setTextSize(17);
dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(Color.BLACK);
設定自定義view:
AlertDialog dialog = builder.create();
dialog.show();
dialog.setContentView(R.layout.activity_main);
TextView textView = dialog.findViewById(R.id.idtv);
textView.setText("sldfjslfjlsfjsf");
textView.setTextColor(Color.RED);
原始碼解釋:
/**
* Set the screen content from a layout resource. The resource will be
* inflated, adding all top-level views to the screen.
*
* @param layoutResID Resource ID to be inflated.
*/
public void setContentView(@LayoutRes int layoutResID) {
mWindow.setContentView(layoutResID);
}
dialog重新獲取事件,進行更新:
dialog.getButton(dialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
設定背景:
dialog.getWindow().setBackgroundDrawable( new ColorDrawable( 0 ) );