General Dialog, custom dialog, general custom dialog
Pattern:
1. Custom dialog usage
YouMengDialog userMsgDialog=null;
Private void initTipDialog (){
UserMsgDialog = new YouMengDialog (this );
UserMsgDialog. setTitle ("system prompt! ");
UserMsgDialog. setMessage ("content prompted! ");
UserMsgDialog. setCanceledOnTouchOutside (false );
UserMsgDialog. setButton1 ("cancel", new View. OnClickListener (){
@ Override
Public void onClick (View v ){
UserMsgDialog. dismiss ();
}
});
UserMsgDialog. setButton2 ("OK", new View. OnClickListener (){
@ Override
Public void onClick (View v ){
UserMsgDialog. dismiss ();
}
});
UserMsgDialog. show ();
}
2. java code of dialog
Public class YouMengDialog extends Dialog {
Private View contentView;
Public YouMengDialog (Context context ){
Super (context, R. style. tran_AlertDialog );
LayoutInflater inflater = LayoutInflater. from (context );
This. contentView = inflater. inflate (R. layout. dialog, null );
}
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (contentView );
Window window = this. getWindow ();
WindowManager. LayoutParams lp = window. getAttributes ();
Display d = window. getWindowManager (). getdefadisplay Display ();
Lp. width = (int) (d. getWidth () * 0.9F );
Window. setAttributes (lp );
}
// Set the prompt box title
Public void setTitle (String title ){
Final TextView textView = (TextView) contentView. findViewById (R. id. title );
TextView. setText (title );
}
// Set the prompt content
Public void setMessage (String message ){
Final TextView textView = (TextView) contentView. findViewById (R. id. message );
TextView. setText (message );
}
// Set the first button (cancel button) in the prompt box, title: Text on the button, onclicklistener: triggered event
Public void setButton (String title, View. OnClickListener onClickListener ){
Final Button button = (Button) contentView. findViewById (R. id. dialogButton );
Button. setText (title );
Button. setOnClickListener (onClickListener );
}
// Hide the cancel key when only one OK key is required
Public void setButtonGone (){
Button button = (Button) contentView. findViewById (R. id. dialogButton );
Button. setVisibility (View. GONE );
}
// Set the second key (OK button) in the prompt box, title: Text on the button, onclicklistener: triggered event
public void setButton2(String title,View.OnClickListener onClickListener){
final Button button = (Button) contentView.findViewById(R.id.dialogButton2);
button.setText(title);
button.setOnClickListener(onClickListener);
}
}
3. Style Sheets Used
<Style name = "tran_AlertDialog" parent = "@ android: style/Theme. Dialog">
<Item name = "android: windowFrame"> @ null </item>
<! -- Border -->
<Item name = "android: incluwisfloating"> true </item>
<! -- Whether to float above the activity -->
<Item name = "android: javaswistranslucent"> true </item>
<! -- Translucent -->
<Item name = "android: windowNoTitle"> true </item>
<! -- No title -->
<Item name = "android: windowBackground"> @ drawable/gray_corners_border </item>
<! -- Background -->
<Item name = "android: backgroundDimEnabled"> true </item>
<! -- Fuzzy -->
</Style>
4. dialog Layout
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
>
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Android: background = "@ drawable/gray_bottom_border"
Android: padding = "10dp"
Android: gravity = "center_vertical">
<ImageView
Android: src = "@ drawable/jinggao"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: adjustViewBounds = "true"
Android: maxWidth = "25dp"
Android: maxHeight = "25dp"
Android: scaleType = "fitCenter"
Android: layout_marginRight = "8dp"/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "prompt message"
Android: id = "@ + id/title"
Android: textColor = "@ color/black"
Android: textSize = "@ dimen/txt_20"/>
</LinearLayout>
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Android: padding = "10dp">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "prompt message"
Android: id = "@ + id/message"
Android: textColor = "@ color/black"
Android: textSize = "@ dimen/txt_20"/>
</LinearLayout>
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "horizontal"
Android: padding = "10dp"
Android: gravity = "center">
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "40dp"
Android: text = "cancel"
Android: background = "@ drawable/gray_corners_button"
Android: id = "@ + id/dialogButton"
Android: layout_marginLeft = "4dp"
Android: layout_marginRight = "4dp"
Android: textSize = "@ dimen/txt_20"
Android: layout_weight = "1"/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "40dp"
Android: text = "OK"
Android: background = "@ drawable/green_corners_button"
Android: id = "@ + id/dialogButton2"
Android: textSize = "@ dimen/txt_20"
Android: textColor = "@ color/white"
Android: layout_marginLeft = "4dp"
Android: layout_marginRight = "4dp"
Android: layout_weight = "1"/>
</LinearLayout>