Android custom dialog box, android custom

Source: Internet
Author: User

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 ();}});}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.