Android custom dialog box Alertdialog using the example _android

Source: Internet
Author: User
Tags gettext

In Android development, we often need to pop up some dialog boxes on the Android interface, such as asking the user or letting the user choose. These features we call it the Android Dialog dialog box, Alertdialog implementation method for Builder mode. Some of the dialogs defined in Alertdialog often fail to satisfy our need for dialogs, and then we need to customize the dialog box view to achieve the requirements, here I customize a login prompt dialog box, the effect of the image shown below:

Layout (Alertdialog Custom login button) interface code:

<?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=" Match_parent ">
  <button
    android:text=" "Custom Login"
    android:layout_width= "Match_parent"
    android: layout_height= "Wrap_content"
    android:id= "@+id/button5" android:onclick=
    "login"/>
</ Linearlayout>

Layout (login_layout Landing window) interface:

<?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=" Match_parent ">
  <edittext
    android:layout_width=" match_parent "
    android:layout_height=" Wrap_ Content "
    android:inputtype=" text "
    android:hint=" Please enter user name
    android:id= "@+id/et_username"/>
  <edittext
    android:layout_width= "match_parent"
    android:layout_height= "Wrap_content"
    android: Inputtype= "Textpassword"
    android:hint= "Please enter password"
    android:id= "@+id/et_password"/>
</ Linearlayout>

Java Functionality Implementation code:

public class Alertdialogdemo extends Appcompatactivity {private EditText Et_username,et_password;
    @Override protected void OnCreate (@Nullable Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.alertdialog);
    public void Login (View v) {alertdialog.builder Builder = new Alertdialog.builder (this); 
    Builder.settitle ("login"); 
    The login_layout View view = Getlayoutinflater (). Inflate (R.layout.login_layout,null) is obtained by the layout filler
    Get two text edit box (password does not do login here to implement, demo only) final EditText et_username = (edittext) View.findviewbyid (r.id.et_username);
    Final EditText Et_password = (edittext) View.findviewbyid (R.id.et_password); Builder.setview (view);/set Login_layout as dialog prompt builder.setcancelable (false);//set to not cancel//Set the front button and do event handling builder.se Tpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface di aloginterface, int i) {String name = Et_username.gettext (). tOstring (). Trim ();
        String pass = Et_password.gettext (). toString (). Trim ();
      Toast.maketext (alertdialogdemo.this,name + "Login ...", toast.length_short). Show ();
    }
    }); Set the reverse button and do event handling Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public V OID OnClick (dialoginterface dialoginterface, int i) {Toast.maketext (alertdialogdemo.this, "Cancel Login", Toast.length_shor
      T). Show ();
    }
    }); Builder.show ()///Show Dialog dialog box}}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.