Android 登陆、提交数据或者加载数据时提示页面

本案例比较简单,就是使用activity自定义成Dialog样式的加载页面,就是弹出框形式。

第一步:我们先对加载页面的样式进行定义MyDialogStyle,在styles.xml文件中,如下:

Xml代码   收藏代码
  1. <style name="MyDialogStyle">  
  2.        <item name="android:windowBackground">@android :color/transparent</item><!--背景透明-->  
  3.        <item name="android:windowFrame">@null </item><!--边框-->  
  4.        <item name="android:windowNoTitle">true</item><!--无标题-->  
  5.        <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->  
  6.        <item name="android:windowIsTranslucent">true</item><!--半透明-->  
  7.        <item name="android:windowContentOverlay">@null </item><!--内容覆盖 -->  
  8.        <item name="android:windowAnimationStyle">@android :style/Animation.Dialog</item><!-- 窗口样式Dialog -->  
  9.        <item name="android:backgroundDimEnabled">true</item><!--模糊-->       
  10.  </style>  

 

第二步;设计加载页面的xml布局loading.xml,比较简单直接代码:

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.     >    
  6.   
  7.     <RelativeLayout  
  8.         android:layout_width="180dp"  
  9.         android:layout_height="180dp"  
  10.         android:layout_centerInParent="true"  
  11.         android:background="@drawable/loading_bg" >  
  12.   
  13.         <LinearLayout  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="fill_parent"  
  16.             android:gravity="center"  
  17.             android:orientation="vertical" >  
  18.               
  19.             <ProgressBar  
  20.                 android:id="@+id/progressBar1"  
  21.                 style="?android:attr/progressBarStyleLarge"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content"          
  24.                 android:layout_gravity="center_horizontal"                
  25.             />        
  26.             <TextView  
  27.                 android:layout_width="wrap_content"  
  28.                 android:layout_height="wrap_content"  
  29.                 android:text="正在登录"  
  30.                 android:layout_marginTop="10dp"  
  31.                 android:textColor="#fff"  
  32.                 android:textSize="20sp"  
  33.         />  
  34.         </LinearLayout>  
  35.   
  36.     </RelativeLayout>  
  37.   
  38. </RelativeLayout>  

 

第三步:创建LoadingActivity类继承Activity,我这里没有做任何操作,根据需要自己设计,这里只是添加了等待并销毁操作,代码如下:


Java代码   收藏代码
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.os.Handler;  
  4. import android.widget.Toast;  
  5.   
  6. public class LoadingActivity extends Activity{  
  7.   
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);   
  11.         setContentView(R.layout.loading);  
  12.       
  13.     //这里Handler的postDelayed方法,等待10000毫秒在执行run方法。  
  14.     //在Activity中我们经常需要使用Handler方法更新UI或者执行一些耗时事件,  
  15.     //并且Handler中post方法既可以执行耗时事件也可以做一些UI更新的事情,比较好用,推荐使用  
  16.     new Handler().postDelayed(new Runnable(){  
  17.         public void run(){    
  18.             //等待10000毫秒后销毁此页面,并提示登陆成功  
  19.             LoadingActivity.this.finish();  
  20.             Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();  
  21.         }  
  22.     }, 10000);  
  23.    }  
  24. }  
 

第四步:在首页中只是用了一个按钮启动加载页:


Java代码   收藏代码
  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7.   
  8. public class MainActivity extends Activity implements OnClickListener{  
  9.   
  10.     private Button main_login_btn;  
  11.       
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         main_login_btn = (Button) this.findViewById(R.id.main_login_btn);  
  17.         main_login_btn.setOnClickListener(this);  
  18.     }  
  19.   
  20.     public void onClick(View v) {  
  21.         Intent intent = new Intent();  
  22.         intent.setClass(MainActivity.this,LoadingActivity.class);//跳转到加载界面  
  23.         startActivity(intent);        
  24.     }  
  25.       
  26. }  
 

第五步;运行效果如下:


转自http://104zz.iteye.com/blog/1688249

转载于:https://my.oschina.net/u/1388249/blog/176611

可根据加载情况显示提示信息的载入视图,如,加载失败后提示"加载失败"、"网络连接失败"等等。用一个控件就可以管理整个的加载过程。确实方便了。项目地址:https://github.com/antonkrasov/AndroidProgressLayout 效果图:如何使用将<com.github.androidprogresslayout.ProgressLayout> 作为整个布局的根:<com.github.androidprogresslayout.ProgressLayout         xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:app="http://schemas.android.com/apk/res-auto"         android:id="@ id/progress_layout"         android:layout_width="match_parent"         android:layout_height="match_parent"               >     ...你的页面 </com.github.androidprogresslayout.ProgressLayout>2. 得到ProgressLayoutProgressLayout progressLayout = (ProgressLayout) this.findViewById(R.id.progress_layout);3. 通过progressLayout.showContent()来打开loading,或者你也可以使用属性:app:progress="true"mHandler.postDelayed(new Runnable() {     @Override     public void run() { //progressLayout.showContent();//加载成功,关闭loading progressLayout.showErrorText("加载失败");//加载失败,并显示提示信息     } }, 2000);利用Handler模拟了一个2秒的载入过程。如果载入成功,调用progressLayout.showContent()关闭loading。如果载入失败,调用progressLayout.showErrorText("加载失败")显示失败信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值