Android login waiting effect, android login waiting
In the previous article, I shared the usage of AsyncTask. This article, in combination with AsyncTask, describes a common effect that we often see. When we click log on, a small wait form will pop up, how is this effect achieved? This article is a simple example.
First, see:
After entering the personal information, click log on and enter the interface. Now let's implement it together.
Step 1: 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" 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 = "10" 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 = "10" 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 = ""/> </RelativeLayout>
Step 2: Main Activity:
Public class MainActivity extends Activity implements OnClickListener {private EditText mEditText1; private EditText mEditText2; private Button mButton1; private Button mButton2; private String email; private String password; private myAsyncTast tast; private ProgressDialog dialog = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init (); // initialize the control} private void init () {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 void onClick (View arg0) {switch (arg0.getId () {case R. id. button1: getEditTextValue (); // obtain the user's input tast = new myAsyncTast (); // create AsyncTask tast.exe cute (); // start AsyncTask break; case R. id. button2: Toast. makeText (MainActivity. this, "register", Toast. LENGTH_SHORT ). show (); break ;}} private void getEditTextValue () {email = mEditText1.getText (). toString (); password = mEditText2.getText (). toString ();} class myAsyncTast extends AsyncTask <Void, Integer, Void >{@ Override protected void onPreExecute () {super. onPreExecute (); dialog = ProgressDialog. show (MainActivity. this, "Logon prompt", "logging in... please wait... ", false); // create ProgressDialog} @ Override protected Void doInBackground (Void... arg0) {Http http = new Http (); int n = http. send (email, password); // send it to publishProgress (n); return null ;}@ Override protected void onProgressUpdate (Integer... values) {super. onProgressUpdate (values); dialog. dismiss (); // close ProgressDialog if (values [0] = 1) {Toast. makeText (MainActivity. this, "Logon successful", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (MainActivity. this, "Logon Failed", Toast. LENGTH_SHORT ). show ();}}}}
Step 3: Server (for simplicity, just simulate)
/** Simulate server */public class Http {private int n = 0; public int send (String email, String password) {try {Thread. sleep (5000); // simulate network loading} catch (InterruptedException e) {e. printStackTrace ();} if (email. equals ("1@qq.com") & password. equals ("123456") {n = 1;} return n ;}}
In this case, we need to declare the network access permission in the AndroidManifest. xml file when sending data to the server during actual development.
For the above content, AsyncTask has already been described in detail in the previous article. If you are eager to read this blog, Please study it carefully and I will not go into details here, this is my first time using ProgressDialog, but you can recommend a blog:.