上一篇為大家分享了關於AsyncTask的使用,本篇結合AsyncTask為大家介紹一個我們經常看到的一個效果,就是當我們點擊登錄后,會彈出一個請等待的小窗體,這個效果是如何實現的呢?本篇我就帶大家簡單實現一下。
首先請看效果圖:
就是當我們填寫好個人信息后,點擊登錄,然后就進入了這個界面,好了,下面我們一起來實現一下。
第一步:布局文件: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="郵箱:" /> <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="密碼:" /> <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="登錄" /> <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>
第二步:主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();//對控件進行初始化 } 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();//獲得用戶的輸入 tast = new myAsyncTast();//創建AsyncTask tast.execute();//啟動AsyncTask break; case R.id.button2: Toast.makeText(MainActivity.this, "注冊", 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, "登錄提示", "正在登錄,請稍等...", false);//創建ProgressDialog } @Override protected Void doInBackground(Void... arg0) { Http http = new Http(); int n = http.send(email, password);//發送給服務器 publishProgress(n); return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); dialog.dismiss();//關閉ProgressDialog if(values[0]==1){ Toast.makeText(MainActivity.this, "登錄成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show(); } } } }
第三步:服務器端(簡單起見,僅僅是模擬)
/* * 模擬服務器端 */ public class Http { private int n = 0; public int send(String email, String password){ try { Thread.sleep(5000);//模擬網絡加載 } catch (InterruptedException e) { e.printStackTrace(); } if(email.equals("1@qq.com")&&password.equals("123456")){ n=1; } return n; } }
在這里需要說明的時,當我們真正開發時,需要向服務器發送數據時,我們需要在:AndroidManifest.xml文件中聲明訪問網絡權限。
對於上面的內容,AsyncTask上一篇已經為大家進行了詳細的介紹,如果看本篇博客你趕腳有些吃力,請仔細研究一下上一篇的博客,這里就不再贅述,對於ProgressDialog本人也是初次使用,不過可以為大家推薦一篇博客:http://blog.csdn.net/caesardadi/article/details/11982721,這里介紹的非常的詳細,有興趣的童鞋可以好好研究一下。