天天看點

android dialog對話框,Android Dialog對話框詳解

廢話不多說了,直接給大家貼代碼了。

布局檔案xml:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".DialogActivity" >

android:id="@+id/plainDialog"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="普通Dialog" />

android:id="@+id/plainDialogEvent"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Dialog按鈕事件集中處理" />

android:id="@+id/inputDialog"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="請輸入框" />

android:id="@+id/listDialog"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="清單對話框" />

android:id="@+id/radioDialog"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="單選對話框" />

android:id="@+id/checkboxDialog"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="多選對話框" />

android:id="@+id/diyDialog"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="自定義布局對話框" />

Activity檔案:

普通的dialog:

private void plainDialogDemo() {

Button plainBtn = (Button) findViewById(R.id.plainDialog);

plainBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

new AlertDialog.Builder(DialogActivity.this)

.setTitle("删除")

.setMessage("确定删除指定資料")

.setPositiveButton("确定",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

Toast.makeText(getApplicationContext(),

"确定了", Toast.LENGTH_SHORT)

.show();

}

})

.setNegativeButton("取消",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

}

}).setCancelable(false).show();

}

});

}

效果如下:

android dialog對話框,Android Dialog對話框詳解

輸入文本框的dialog:

private void inputDialog() {

Button inputBtn = (Button) findViewById(R.id.inputDialog);

inputBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

final EditText et = new EditText(DialogActivity.this);

new AlertDialog.Builder(DialogActivity.this)

.setTitle("請輸入數字")

.setView(et)

.setPositiveButton("确定",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(),

et.getText(),

Toast.LENGTH_SHORT).show();

}

}).setNegativeButton("取消", null)

.setCancelable(false).show();

}

});

}

效果如下:

android dialog對話框,Android Dialog對話框詳解

清單dialog:

private void listDialogDemo() {

Button listBtn = (Button) findViewById(R.id.listDialog);

listBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

final String[] names = { "C羅", "J羅", "H羅" };

new AlertDialog.Builder(DialogActivity.this).setTitle("清單對話框")

.setItems(names, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

Toast.makeText(DialogActivity.this,

names[which], Toast.LENGTH_SHORT)

.show();

}

}).setNegativeButton("取消", null).show();

}

});

}

效果如下:

android dialog對話框,Android Dialog對話框詳解

單選dialog:

private void radioDialogDemo() {

Button radioButton = (Button) findViewById(R.id.radioDialog);

radioButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

final String[] names = { "C羅", "J羅", "H羅" };

new AlertDialog.Builder(DialogActivity.this)

.setTitle("清單對話框")

.setSingleChoiceItems(names, ,

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

selecteName = names[which];

}

})

.setPositiveButton("确定",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

Toast.makeText(DialogActivity.this,

selecteName, Toast.LENGTH_SHORT)

.show();

}

}).setNegativeButton("取消", null).show();

}

});

}

效果如下:

android dialog對話框,Android Dialog對話框詳解

多選dialog:

private void checkDialogDemo() {

Button checkBtn = (Button) findViewById(R.id.checkboxDialog);

checkBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

final String[] names = { "C羅", "J羅", "H羅" };

final boolean[] selected = new boolean[] { true, false, true };

new AlertDialog.Builder(DialogActivity.this)

.setMultiChoiceItems(

names,

selected,

new DialogInterface.OnMultiChoiceClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which, boolean isChecked) {

}

})

.setPositiveButton("确定",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

StringBuilder sb = new StringBuilder(

"你選擇了:");

for (int i = ; i < names.length; i++) {

if (selected[i]) {

sb.append(names[i]);

}

}

Toast.makeText(DialogActivity.this,

sb.toString(), ).show();

}

}).setNegativeButton("取消", null).show();

}

});

}

效果如下:

android dialog對話框,Android Dialog對話框詳解

自定義dialog:

private void customDialogDemo() {

final AlertDialog dlg = new AlertDialog.Builder(this).create();

dlg.show();

Window window = dlg.getWindow();

window.setContentView(R.layout.diylayout);

ImageButton ok = (ImageButton) window.findViewById(R.id.btnok);

ok.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(), "關閉了",

Toast.LENGTH_SHORT).show();

dlg.dismiss();

}

});

}

自定義布局:

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/dialogimg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:src="@drawable/dialog_bg" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@id/dialogimg"

android:layout_alignTop="@id/dialogimg"

android:layout_marginLeft="dp"

android:layout_marginTop="dp"

android:text="自定義的dialog" />

android:id="@+id/btnok"

android:layout_width="dp"

android:layout_height="dp"

android:layout_alignRight="@id/dialogimg"

android:layout_alignTop="@id/dialogimg"

android:layout_marginRight="dp"

android:layout_marginTop="dp"

android:background="@drawable/close_dialog" />

效果如:

android dialog對話框,Android Dialog對話框詳解

有關Android Dialog對話框詳解小編就給大家介紹這麼多,希望對大家有所幫助!

繼續閱讀