- 論壇徽章:
- 0
|
首先請看效果圖:
1.png (17.65 KB, 下載次數(shù): 62)
下載附件
2015-06-02 11:14 上傳
就是當(dāng)我們填寫好個人信息后,點擊登錄,然后就進(jìn)入了這個界面,好了,下面我們一起來實現(xiàn)一下。
第一步:布局文件: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>
復(fù)制代碼 第二步:主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();//對控件進(jìn)行初始化
- }
- 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();//創(chuàng)建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);//創(chuàng)建ProgressDialog
- }
-
- @Override
- protected Void doInBackground(Void... arg0) {
-
- Http http = new Http();
- int n = http.send(email, password);//發(fā)送給服務(wù)器
- publishProgress(n);
- return null;
-
- }
-
- @Override
- protected void onProgressUpdate(Integer... values) {
- super.onProgressUpdate(values);
- dialog.dismiss();//關(guān)閉ProgressDialog
- if(values[0]==1){
- Toast.makeText(MainActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(MainActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show();
- }
-
- }
-
- }
-
- }
復(fù)制代碼 第三步:服務(wù)器端(簡單起見,僅僅是模擬)- /*
- * 模擬服務(wù)器端
- */
- public class Http {
- private int n = 0;
- public int send(String email, String password){
-
- try {
- Thread.sleep(5000);//模擬網(wǎng)絡(luò)加載
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if(email.equals("1@qq.com")&&password.equals("123456")){
- n=1;
- }
-
- return n;
- }
-
- }
復(fù)制代碼 在這里需要說明的時,當(dāng)我們真正開發(fā)時,需要向服務(wù)器發(fā)送數(shù)據(jù)時,我們需要在:AndroidManifest.xml文件中聲明訪問網(wǎng)絡(luò)權(quán)限。 |
|