On an article for everyone to share about the use of asynctask, this article combined with Asynctask for everyone to introduce a we often see an effect, that is, when we click Login, will pop up a wait for the small form, how this effect is implemented? In this article I will bring you a simple implementation.
First, look at:
is when we fill in the personal information, click Login, and then entered the interface, OK, let us come together to achieve.
First step: Layout file: Activity_main.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"Android:paddingbottom="@dimen/activity_vertical_margin"Android:paddingleft="@dimen/activity_horizontal_margin"Android:paddingright="@dimen/activity_horizontal_margin"Android:paddingtop="@dimen/activity_vertical_margin"Tools:context=". Mainactivity"> <TextView Android:id="@+id/textview1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignparentleft="true"Android:layout_alignparenttop="true"Android:layout_margintop="22DP"Android:text="Email:"/> <EditText Android:id="@+id/edittext1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignbaseline="@+id/textview1"Android:layout_alignbottom="@+id/textview1"Android:layout_marginleft="15DP"Android:layout_torightof="@+id/textview1"Android:ems="Ten"Android:inputtype="textemailaddress"/> <TextView Android:id="@+id/textview2"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_below="@+id/edittext1"Android:layout_margintop="40DP"Android:layout_toleftof="@+id/edittext1"Android:text="Password:"/> <EditText Android:id="@+id/edittext2"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignbaseline="@+id/textview2"Android:layout_alignbottom="@+id/textview2"Android:layout_alignleft="@+id/edittext1"Android:ems="Ten"Android:inputtype="Textpassword"/> <Button Android:id="@+id/button1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_below="@+id/edittext2"Android:layout_margintop="43DP"Android:layout_torightof="@+id/textview2"Android:text="Login"/> <Button Android:id="@+id/button2"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignbaseline="@+id/button1"Android:layout_alignbottom="@+id/button1"Android:layout_marginleft="34DP"Android:layout_torightof="@+id/button1"Android:text="Register"/></relativelayout>
Step Two: Main activity:
Public classMainactivity extends Activity implements onclicklistener{PrivateEditText MEditText1; PrivateEditText mEditText2; PrivateButton MButton1; PrivateButton MButton2; PrivateString Email; PrivateString password; PrivateMyasynctast tast; PrivateProgressDialog dialog =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Init ();//Initializing a control } Private voidinit () {MEditText1=(EditText) Findviewbyid (R.ID.EDITTEXT1); MEditText2=(EditText) Findviewbyid (R.ID.EDITTEXT2); MButton1=(Button) Findviewbyid (R.id.button1); MButton2=(Button) Findviewbyid (R.id.button2); Mbutton1.setonclicklistener ( This); Mbutton2.setonclicklistener ( This); } @Override Public voidOnClick (View arg0) {Switch(Arg0.getid ()) { CaseR.id.button1:getedittextvalue ();//get the user's inputtast =NewMyasynctast ();//Create AsynctaskTast.execute ();//Start Asynctask Break; CaseR.id.button2:toast.maketext (mainactivity. This,"Register", Toast.length_short). Show (); Break; } } Private voidGetedittextvalue () {Email=Medittext1.gettext (). toString (); Password=Medittext2.gettext (). toString (); } classMyasynctast extends Asynctask<void, Integer, void>{@Overrideprotected voidOnPreExecute () {super.onpreexecute (); Dialog= Progressdialog.show (mainactivity. This,"Login Prompt","logging in, please wait ...",false);//Create ProgressDialog} @Overrideprotectedvoid Doinbackground (void ... arg0) {http HTTP=NewHttp (); intn = http.send (email, password);//Send to Serverpublishprogress (n); return NULL; } @Overrideprotected voidonprogressupdate (Integer ... values) {super.onprogressupdate (values); Dialog.dismiss ();//Close ProgressDialog if(values[0]==1) {Toast.maketext (mainactivity. This,"Login Successful", Toast.length_short). Show (); }Else{toast.maketext (mainactivity). This,"Logon Failure", Toast.length_short). Show (); } } } }
Step Three: Server side (simple, simulation only)
/** Analog Server side*/ Public classHttp {Private intn =0; Public intSend (string email, string password) {Try{Thread.Sleep ( the);//Analog Network loading}Catch(interruptedexception e) {e.printstacktrace (); } if(Email.equals ("[email protected]") &&password.equals ("123456") ) {n=1; } returnN; } }
When we need to explain this, when we really develop, we need to send data to the server, we need to declare the access network permissions in the: Androidmanifest.xml file.
For the above content, Asynctask on an article has been a detailed introduction for everyone, if you look at this blog bright Some difficulties, please carefully study the previous blog, here will not repeat, for ProgressDialog himself is also the first use, However, we can recommend a blog: http://blog.csdn.net/caesardadi/article/details/11982721, here is the very detailed, interested in children's shoes can be a good look.
Android Login Wait effect