免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2069 | 回复: 0
打印 上一主题 下一主题

[Android] Android登录等待效果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-02 11:15 |只看该作者 |倒序浏览
首先请看效果图:

就是当我们填写好个人信息后,点击登录,然后就进入了这个界面,好了,下面我们一起来实现一下。

  第一步:布局文件:activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <TextView
  11.         android:id="@+id/textView1"
  12.         android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content"
  14.         android:layout_alignParentLeft="true"
  15.         android:layout_alignParentTop="true"
  16.         android:layout_marginTop="22dp"
  17.         android:text="邮箱:" />

  18.     <EditText
  19.         android:id="@+id/editText1"
  20.         android:layout_width="wrap_content"
  21.         android:layout_height="wrap_content"
  22.         android:layout_alignBaseline="@+id/textView1"
  23.         android:layout_alignBottom="@+id/textView1"
  24.         android:layout_marginLeft="15dp"
  25.         android:layout_toRightOf="@+id/textView1"
  26.         android:ems="10"
  27.         android:inputType="textEmailAddress" />

  28.     <TextView
  29.         android:id="@+id/textView2"
  30.         android:layout_width="wrap_content"
  31.         android:layout_height="wrap_content"
  32.         android:layout_below="@+id/editText1"
  33.         android:layout_marginTop="40dp"
  34.         android:layout_toLeftOf="@+id/editText1"
  35.         android:text="密码:" />

  36.     <EditText
  37.         android:id="@+id/editText2"
  38.         android:layout_width="wrap_content"
  39.         android:layout_height="wrap_content"
  40.         android:layout_alignBaseline="@+id/textView2"
  41.         android:layout_alignBottom="@+id/textView2"
  42.         android:layout_alignLeft="@+id/editText1"
  43.         android:ems="10"
  44.         android:inputType="textPassword" />

  45.     <Button
  46.         android:id="@+id/button1"
  47.         android:layout_width="wrap_content"
  48.         android:layout_height="wrap_content"
  49.         android:layout_below="@+id/editText2"
  50.         android:layout_marginTop="43dp"
  51.         android:layout_toRightOf="@+id/textView2"
  52.         android:text="登录" />

  53.     <Button
  54.         android:id="@+id/button2"
  55.         android:layout_width="wrap_content"
  56.         android:layout_height="wrap_content"
  57.         android:layout_alignBaseline="@+id/button1"
  58.         android:layout_alignBottom="@+id/button1"
  59.         android:layout_marginLeft="34dp"
  60.         android:layout_toRightOf="@+id/button1"
  61.         android:text="注册" />

  62. </RelativeLayout>
复制代码
第二步:主Activity:
  1. public class MainActivity extends Activity implements OnClickListener{

  2.     private EditText mEditText1;
  3.     private EditText mEditText2;
  4.     private Button mButton1;
  5.     private Button mButton2;
  6.    
  7.     private String email;
  8.     private String password;
  9.    
  10.     private myAsyncTast tast;
  11.    
  12.     private ProgressDialog dialog = null;
  13.    
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_main);
  18.         init();//对控件进行初始化
  19.     }


  20.     private void init() {
  21.         mEditText1 = (EditText) findViewById(R.id.editText1);
  22.         mEditText2 = (EditText) findViewById(R.id.editText2);
  23.         mButton1 = (Button) findViewById(R.id.button1);
  24.         mButton2 = (Button) findViewById(R.id.button2);
  25.         mButton1.setOnClickListener(this);
  26.         mButton2.setOnClickListener(this);
  27.     }


  28.     @Override
  29.     public void onClick(View arg0) {
  30.         switch (arg0.getId()) {
  31.         case R.id.button1:
  32.             getEditTextValue();//获得用户的输入
  33.             tast = new myAsyncTast();//创建AsyncTask
  34.             tast.execute();//启动AsyncTask
  35.             break;

  36.         case R.id.button2:
  37.             Toast.makeText(MainActivity.this, "注册", Toast.LENGTH_SHORT).show();
  38.             break;
  39.         }
  40.     }

  41.     private void getEditTextValue() {
  42.         email = mEditText1.getText().toString();
  43.         password = mEditText2.getText().toString();
  44.     }

  45.     class myAsyncTast extends AsyncTask<Void, Integer, Void>{

  46.         @Override
  47.         protected void onPreExecute() {
  48.             super.onPreExecute();
  49.             dialog = ProgressDialog.show(MainActivity.this, "登录提示", "正在登录,请稍等...", false);//创建ProgressDialog
  50.         }
  51.         
  52.         @Override
  53.         protected Void doInBackground(Void... arg0) {
  54.         
  55.             Http http = new Http();
  56.             int n = http.send(email, password);//发送给服务器
  57.             publishProgress(n);
  58.             return null;
  59.             
  60.         }
  61.         
  62.         @Override
  63.         protected void onProgressUpdate(Integer... values) {
  64.             super.onProgressUpdate(values);
  65.             dialog.dismiss();//关闭ProgressDialog
  66.             if(values[0]==1){
  67.                 Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  68.             }else{
  69.                 Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
  70.             }
  71.             
  72.         }
  73.         
  74.     }
  75.    
  76. }
复制代码
第三步:服务器端(简单起见,仅仅是模拟)
  1. /*
  2. * 模拟服务器端
  3. */
  4. public class Http {
  5.     private int n = 0;
  6.     public int send(String email, String password){
  7.         
  8.         try {
  9.             Thread.sleep(5000);//模拟网络加载
  10.         } catch (InterruptedException e) {
  11.             e.printStackTrace();
  12.         }
  13.         if(email.equals("1@qq.com")&&password.equals("123456")){
  14.             n=1;
  15.         }
  16.         
  17.         return n;
  18.     }
  19.    
  20. }
复制代码
在这里需要说明的时,当我们真正开发时,需要向服务器发送数据时,我们需要在:AndroidManifest.xml文件中声明访问网络权限。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP