Android登入等待效果,android登入等待

來源:互聯網
上載者:User

Android登入等待效果,android登入等待

  上一篇為大家分享了關於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,這裡介紹的非常的詳細,有興趣的童鞋可以好好研究一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.