直接進入主題 :
使用AlertDialog的多種方式
要創建一個AlertDialog,就要用到AlertDialog.Builder中的Create()方法,
其中AlertDialog.Builder提供了這幾種常用的方法:
setTile()爲對話框設置一個標題
setMessage()爲對話框設置內容
setIcon()爲對話框設置顯示圖片
setView()爲對話框自定義樣式
setItems()爲對話框顯示的一個list,一般用於顯示幾個命令時
setPositiveButton()給對話框添加“確認”按鈕
setNegativeButton()給對話框添加“取消”按鈕
第一種: 確認對話框
怎麼實現呢 ?
protected void Affirmdialog() {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("this is a title");
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage("this is a message");
//是否可撤銷
builder.setCancelable(false);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Onclick is Ok", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Onclick is NO", Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
第二種: 單選按鈕
setSengleCoiceItems():設置單選按鈕
private String[] Singledata = {"boy", "girl"};
protected void Singledialog() {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setSingleChoiceItems(Singledata, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// TODO Auto-generated method stub
String value = Singledata[i];
Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
第三種 :多選按鈕
setMultiChoiceItems():設置多選對話框
private String[] Multidata = {"Basketball","foolball","badminton"};
protected void Multipledialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("this is a MultipleDialogButton");
builder.setMultiChoiceItems(Multidata, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
if(b){
String value = Multidata[i];
Toast.makeText(MainActivity.this, "I Like" + value, Toast.LENGTH_SHORT).show();
}else{
String value = Multidata[i];
Toast.makeText(MainActivity.this, "I No Like " + value, Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int arg1) {
// TODO Auto-generated method stub
//取消
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
第四種 : 自定義對話框
從而我們可以在自定義的過程加入廣告
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is a customdialog"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/guangkao"/>
</LinearLayout>
protected void Mustomdialog() {
// TODO Auto-generated method stub
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.customdialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(view);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
好了今天的四種AlertDialog對話框實現了,希望大家多多支持,謝謝!