my_dialog.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minWidth="280dip" android:background="#FFFFFFFF"> <TextView android:id="@+id/dialog_title" android:layout_width="fill_parent" android:layout_height="40.0dip" android:gravity="center_vertical" android:paddingLeft="10.0dip" android:text="@string/dialog_title" android:textSize="20dip" android:textStyle="bold" android:textColor="#0087CB"/> <ImageView android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="@drawable/dialog_sparator"/> <LinearLayout android:id="@+id/dialog_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="fill_parent"> <TextView android:id="@+id/dialog_message" android:layout_width="fill_parent" android:layout_height="50.0dip" android:gravity="center_vertical" android:paddingLeft="10dip" android:text="@string/dialog_msg" android:textSize="20dip" android:textStyle="bold" android:textColor="#707070"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="50.0dip" android:layout_alignParentTop="true" android:gravity="center"> <Button android:id="@+id/dialog_back" android:layout_width="120dip" android:layout_height="wrap_content" android:layout_marginRight="5dip" android:text="@string/dialog_back" android:textSize="20dip" android:textColor="#ffffff" android:textStyle="bold" android:background="@drawable/dlg_bckbtn_selector"/> <Button android:id="@+id/dialog_confirm" android:layout_width="120dip" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:text="@string/dialog_exit" android:textSize="20dip" android:textColor="#ffffff" android:textStyle="bold" android:background="@drawable/dlg_cfgrmbtn_selector"/> </LinearLayout> </LinearLayout>
定义Dialog的样式(主题):
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 定义对话框样式 --> <style name="Dialog" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> </style> </resources>
自定义Dialog的Java代码:
import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class MyDialog extends Dialog { public MyDialog(Context context, int theme) { super(context, theme); } public MyDialog(Context context) { super(context); } /** * Helper class for creating a custom dialog */ public static class Builder { private Context context; private String title; // 对话框标题 private String message; // 对话框内容 private String backButtonText; // 对话框返回按钮文本 private String confirmButtonText; // 对话框确定文本 private View contentView; // 对话框按钮监听事件 private DialogInterface.OnClickListener backButtonClickListener, confirmButtonClickListener; public Builder(Context context) { this.context = context; } /** * 使用字符串设置对话框消息 * @param title * @return */ public Builder setMessage(String message) { this.message = message; return this; } /** * 使用资源设置对话框消息 * @param title * @return */ public Builder setMessage(int message) { this.message = (String) context.getText(message); return this; } /** * 使用资源设置对话框标题信息 * @param title * @return */ public Builder setTitle(int title) { this.title = (String) context.getText(title); return this; } /** * 使用字符串设置对话框标题信息 * @param title * @return */ public Builder setTitle(String title) { this.title = title; return this; } /** * 设置自定义的对话框内容 * @param v * @return */ public Builder setContentView(View v) { this.contentView = v; return this; } /** * 设置back按钮的事件和文本 * @param backButtonText * @param listener * @return */ public Builder setBackButton(int backButtonText, DialogInterface.OnClickListener listener) { this.backButtonText = (String)context.getText(backButtonText); this.backButtonClickListener = listener; return this; } /** * 设置back按钮的事件和文本 * @param backButtonText * @param listener * @return */ public Builder setBackButton(String backButtonText, DialogInterface.OnClickListener listener) { this.backButtonText = backButtonText; this.backButtonClickListener = listener; return this; } /** * 设置确定按钮事件和文本 * @param confirmButtonText * @param listener * @return */ public Builder setConfirmButton(int confirmButtonText, DialogInterface.OnClickListener listener) { this.confirmButtonText = (String)context.getText(confirmButtonText); this.confirmButtonClickListener = listener; return this; } /** * 设置确定按钮事件和文本 * @param negativeButtonText * @param listener * @return */ public Builder setConfirmButton(String confirmButtonText, DialogInterface.OnClickListener listener) { this.confirmButtonText = confirmButtonText; this.confirmButtonClickListener = listener; return this; } /** * 创建自定义的对话框 */ public MyDialog create() { LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 实例化自定义的对话框主题 final MyDialog dialog = new MyDialog(context, R.style.Dialog); View layout = inflater.inflate(R.layout.my_dialog, null); dialog.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // 设置对话框标题 ((TextView) layout.findViewById(R.id.dialog_title)).setText(title); // 设置对话框内容 if (message != null) { TextView dlgMsg = (TextView)layout.findViewById(R.id.dialog_message); dlgMsg.setText(message); } else if (contentView != null) { // if no message set // 如果没有设置对话框内容,添加contentView到对话框主体 ((LinearLayout) layout.findViewById(R.id.dialog_content)).removeAllViews(); ((LinearLayout) layout.findViewById(R.id.dialog_content)).addView( contentView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); } // 设置返回按钮事件和文本 if (backButtonText != null) { Button bckButton = ((Button) layout.findViewById(R.id.dialog_back)); bckButton.setText(backButtonText); if (backButtonClickListener != null) { bckButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { backButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { layout.findViewById(R.id.dialog_back).setVisibility(View.GONE); } // 设置确定按钮事件和文本 if (confirmButtonText != null) { Button cfmButton = ((Button) layout.findViewById(R.id.dialog_confirm)); cfmButton.setText(confirmButtonText); if (confirmButtonClickListener != null) { cfmButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { confirmButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { layout.findViewById(R.id.dialog_confirm).setVisibility(View.GONE); } dialog.setContentView(layout); return dialog; } } }
MainActivity.java:
package com.example.stuart.customdialog; 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.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Mydialog mydialog; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn =(Button)findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Mydialog.Builder builder =new Mydialog.Builder(MainActivity.this); builder.setTitle("提示"); builder.setMessage("你要删除吗?!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); builder.setConfirmButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); builder.setBackButton("返回",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { mydialog.dismiss(); } }) ; mydialog=builder.create(); mydialog.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.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }