Android custom dialog box, android custom
There is a built-in dialog box in android. For the sake of appearance, many developers will use a custom dialog box, such:
Click the "pop-up custom dialog box" to display the effect.
First, you must define an xml file to define the style of your dialog box:
<? 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: background = "@ drawable/background" android: orientation = "vertical"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginLeft = "10dp" android: layout_marginRight = "10dp" android: orientation = "horiz Ontal "> <ImageView android: layout_width =" 0dp "android: layout_height =" wrap_content "android: layout_weight =" 1 "android: src = "@ drawable/airsign"/> <TextView android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_gravity = "center" android: layout_weight = "3" android: gravity = "center" android: text = "enter the password" android: textColor = "# FFFFFFFF" android: textSize = "32px" android: textStyle = "bold "/> </LinearLayout> <EditText android: id =" @ + id/et_pass "android: layout_width =" match_parent "android: layout_height =" 40dp "android: layout_marginLeft = "10dp" android: layout_marginRight = "10dp" android: background = "@ drawable/TV _background" android: paddingLeft = "10dp"/> <LinearLayout android: layout_marginTop = "10dp" android: layout_marginBottom = "10dp" android: layout_marginLeft = "10dp" android: layout_marginRi Ght = "10dp" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/btn_ OK" android: layout_width = "wrap_content" android: layout_height = "40dp" style = "? Android: attr/buttonStyleSmall "android: layout_weight =" 1 "android: background =" @ drawable/background "android: textColor =" # FFFFFFFF "android: text = "OK"/> <Button android: id = "@ + id/btn_delete" android: layout_marginLeft = "20dp" android: layout_width = "wrap_content" android: layout_height = "40dp" style = "? Android: attr/buttonStyleSmall "android: layout_weight =" 1 "android: textColor =" # FFFFFFFF "android: background =" @ drawable/background "android: text = "cancel"/> </LinearLayout>
MainActivity:
Public class MainActivity extends Activity {private Context context = MainActivity. this; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void clickView (View v) {AlertDialog. builder builder = new AlertDialog. builder (this); // create dialog box AlertDialog dialog = builder. create (); // without the following code, the custom dialog box still has the original background builder. setView (View. inflate (this, R. layout. auto_dialog, null); // the dialog box dialog is displayed. show (); // The following two lines of code are the EditText of the dialog box. getWindow (). clearFlags (WindowManager. layoutParams. FLAG_NOT_FOCUSABLE | WindowManager. layoutParams. FLAG_ALT_FOCUSABLE_IM); dialog. getWindow (). setSoftInputMode (WindowManager. layoutParams. SOFT_INPUT_STATE_VISIBLE); // *** this effect is implemented here. // set the content page of the Window. The shrew_exit_dialog.xml file defines the view content window Window = dialog. getWindow (); window. setContentView (R. layout. auto_dialog); EditText et_pass = (EditText) window. findViewById (R. id. et_pass); final Button btn_ OK = (Button) window. findViewById (R. id. btn_ OK); final Button btn_delete = (Button) window. findViewById (R. id. btn_delete); // Add the clicked listening event btn_ OK .setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) for the two button buttons) {// TODO Auto-generated method stub Toast. makeText (context, btn_ OK .getText (), 0 ). show () ;}}); btn_delete.setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub Toast. makeText (context, btn_delete.getText (), 0 ). show ();}});}}