Android: Create a Common Dialog Box and use the dialog box for login

Source: Internet
Author: User

1. First, let's talk about the "prompt" -- Toast

The so-called "prompt" is a simple message prompt box that cannot be clicked by users. It disappears automatically according to the time parameter settings in the code.

 

Create toast

Method 1: maketext (context, int resid, int duration)

Method 2: maketext (context, charsequence text, int duration)


Parameters:

The context parameter indicates the context in which toast is displayed, usually the current activity.

The third parameter duration indicates the display time of toast. By default, Toast has two constants: length_short and length_long, which indicate short-time display and long-time display respectively. You can also set the time by yourself. Note that the unit of time is: milliseconds.

The second parameter can be a string or a prompt information obtained from string. XML in the resource file.

 

// Create a toast object and display it. the second parameter is the string toast. maketext (mainactivity. this, "this is the prompt message", toast. length_long ). show (); // The second parameter is from the resource file string. toast in XML. maketext (mainactivity. this, getstring (R. string. hello), toast. length_short ). show ();

2. Create dialog box (DIALOG)

You can refer to the following links to create a dialog box and some common formats.

Http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html. This blog is well written and provides a comprehensive summary of the dialog box.

 

Below I am doing an extension. The problem is: in the pop-up dialog box, there are two text boxes: user name and password. Enter the correct user name and password and click "OK" to go to the other page. An error is prompted if the user name is incorrect.

 

The following describes how to solve the problem.

 

Most of the above problems occur when logging on to the system, the interface should be as concise as possible. The interface I want to implement is as follows:

 

 

In fact, this is very easy to implement. Let's talk about the interface first. Two activity pages are required. One activity_main.xml is empty, used to hold the dialog box (DIALOG), and the other login_activity.xml contains the input box of the user name and password. Simply put, login_activity.xml is embedded in the dialog.

Let's take a look at the layout file.

Login_activity.xml

<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <textview Android: id = "@ + ID/tvusername" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparentleft = "true" Android: layout_alignparenttop = "true" Android: layout_marginleft = "18dp" Android: layout_margintop = "44dp" Android: text = "username:"/> <edittext Android: Id = "@ + ID/etusername" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignbaseline = "@ + ID/tvusername" Android: layout_alignbottom = "@ + ID/tvusername" Android: layout_marginleft = "56dp" Android: layout_torightof = "@ + ID/tvusername" Android: EMS = "10"> <requestfocus/> </edittext> <textview Android: id = "@ + ID/tvpwd" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignleft = "@ + ID/tvusername" Android: layout_below = "@ + ID/etusername" Android: layout_margintop = "42dp" Android: text = "Password:"/> <edittext Android: id = "@ + ID/etpwd" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignbottom = "@ + ID/tvpwd" Android: layout_alignparentright = "true" Android: EMS = "10"/> </relativelayout>

The following is the Java code, mainly the code of loginactivity.

Package COM. example. testofdialog; import android. OS. bundle; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. content. intent; import android. view. layoutinflater; import android. view. view; import android. widget. edittext; import android. widget. toast; public class loginactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_login); extract () ;}// the public void showwaiterauthorizationdialog () {// layoutinflater is used to find the XML layout file in the layout folder and instantiate layoutinflater factory = layoutinflater. from (loginactivity. this); // define the control in activity_login in view in final view textentryview = factory. inflate (R. layout. activity_login, null); // display the control in loginactivity in the dialog box new alertdialog. builder (loginactivity. this) // the title of the dialog box. settitle ("login") // set the displayed view. setview (textentryview) // click the "login" button in the dialog box. setpositivebutton ("login", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// obtain the user name and password. // Note: textentryview. findviewbyid is very important because the above factory. inflate (R. layout. activity_login, null) assigns the page layout value to textentryview and final edittext etusername = (edittext) textentryview. findviewbyid (R. id. etusername); Final edittext etpassword = (edittext) textentryview. findviewbyid (R. id. etpwd); // convert the "user name" and "password" obtained in the input box to the string username = etusername. gettext (). tostring (). trim (); string Password = etpassword. gettext (). tostring (). trim (); // the user name and password of the role model have been obtained so far, and the next step is to compile the code according to your needs. // a simple test is conducted here, assume that the entered username and password are both 1, then the other operation page (operationactivity) if (username. equals ("1") & password. equals ("1") {// jump to operationactivityintent intent = new intent (); intent. setclass (loginactivity. this, operationactivity. class); startactivity (intent); // close the loginactivity on the current page. this. finish ();} else {toast. maketext (loginactivity. this, "Incorrect password or user name", toast. length_short ). show () ;}}) // click the event in the "exit" dialog box. setnegativebutton ("exit", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int whichbutton) {loginactivity. this. finish () ;}) // create and display the dialog box. create (). show ();}}

After the above, we achieved the expected results. However, there are still some shortcomings: the dialog box will disappear when you click a place other than the dialog box, And the dialog box will disappear after an error message is entered, so you cannot continue the operation.

Let's talk about the question "the dialog box will disappear when you click a place outside the dialog box ".

In fact, the dialog in the Android system is non-modal by default. The above problem can be solved only when the dialog is set to modal. This method is also very simple, that is,. setcancelable (true); you can solve this problem by setting true to false.

 

Let's talk about the question "the dialog box will disappear after an error message is displayed and the operation cannot be continued ".

Call the dismiss () method in the dialog box.

The final code is as follows:

Package COM. example. testofdialog; import Java. lang. reflect. field; import android. OS. bundle; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. content. intent; import android. view. layoutinflater; import android. view. view; import android. widget. edittext; import android. widget. toast; public class loginactivity extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_login); extract () ;}// the public void showwaiterauthorizationdialog () {// layoutinflater is used to find the XML layout file in the layout folder and instantiate layoutinflater factory = layoutinflater. from (loginactivity. this); // define the control in activity_login in view in final view textentryview = factory. inflate (R. layout. activity_login, null); // display the control in loginactivity in the dialog box new alertdialog. builder (loginactivity. this) // the title of the dialog box. settitle ("login") // set the displayed view. setview (textentryview) // click the "login" button in the dialog box. setpositivebutton ("login", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// obtain the user name and password. // Note: textentryview. findviewbyid is very important because the above factory. inflate (R. layout. activity_login, // null) assigns the page layout value to textentryview and final edittext etusername = (edittext) textentryview. findviewbyid (R. id. etusername); Final edittext etpassword = (edittext) textentryview. findviewbyid (R. id. etpwd); // convert the "user name" and "password" obtained in the input box to the string username = etusername. gettext (). tostring (). trim (); string Password = etpassword. gettext (). tostring (). trim (); // the user name and password of the role model have been obtained so far, and the next step is to compile the code according to your needs. // a simple test is conducted here, assume that the entered username and password are both 1, then the other operation page (operationactivity) if (username. equals ("1") & password. equals ("1") {// jump to operationactivityintent intent = new intent (); intent. setclass (loginactivity. this, operationactivity. class); startactivity (intent); // close the loginactivity on the current page. this. finish ();} else {toast. maketext (loginactivity. this, "Incorrect password or user name", toast. length_short ). show (); try {// note that the field mshowing in the source code class is modified to true through reflection. The system will think that the dialog box is opened // and then call dismiss () Field field = dialog. getclass (). getsuperclass (). getdeclaredfield ("mshowing"); field. setaccessible (true); field. set (dialog, false); dialog. dismiss () ;}catch (exception e) {}}}) // click the event in the "exit" dialog box. setnegativebutton ("exit", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int whichbutton) {loginactivity. this. finish () ;}) // set whether the dialog is modal. False indicates modal, and true indicates non-modal. setcancelable (false) // create and display the dialog box. create (). 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.