android(5) 等待窗口

本文介绍了在Android中创建等待窗口的两种方法,包括旋转的ProgressBar和进度条式的等待窗口,以提升用户在数据下载过程中的体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.等待窗口实现:

   记录一下,有时候我们下载数据的时候为了提高用户的体验,加一个等待窗口还是有必要的,有两种等待窗口:

第一种旋转的ProgressBar:

  

  

主界面:

public class MainActivity extends Activity {

	private Button button1;
	private TextView textview1;

	public AlertDialog selfdialog;

	private MainActivity main;
    
	public Handler mhandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			//显示TextView
			case 1:
				Toast.makeText(main, "成功", Toast.LENGTH_SHORT).show();
				textview1.setVisibility(View.VISIBLE);
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		main = this;

		button1 = (Button) findViewById(R.id.button1);
		textview1 = (TextView) findViewById(R.id.textview1);

		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				showDialogLoading();
			}
		});
	}
    //等待dialog
	private void showDialogLoading() {
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(this.LAYOUT_INFLATER_SERVICE);
		//view
		View view = inflater.inflate(R.layout.footer, null);
		AlertDialog.Builder ad = new AlertDialog.Builder(this);
		selfdialog = ad.create();
		selfdialog.setView(view);
		//屏蔽掉点击
		selfdialog.setCancelable(false);
		selfdialog.show();
		new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message message = new Message();
				message.what = 1;
				main.mhandler.sendMessage(message);
				//关闭dialog
				selfdialog.dismiss();
			}
		}.start();
	}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center" >
    <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击我"
        />
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加载完成后显示我"
        android:visibility="gone"
        />
</LinearLayout>

footer.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center" >
    <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击我"
        />
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加载完成后显示我"
        android:visibility="gone"
        />
</LinearLayout>

这样第一种的等待窗就实现了。


第二种进度条式的等待窗:



主界面:

public class MainActivity extends Activity {

	private Button button1;
	private TextView textview1;
	private TextView textview2;
	public AlertDialog selfdialog;

	private MainActivity main;
    
	public Handler mhandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			//显示TextView
			case 1:
				textview2.setText("正在下载("+msg.obj.toString()+"/10)");
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		main = this;

		button1 = (Button) findViewById(R.id.button1);
		textview1 = (TextView) findViewById(R.id.textview1);

		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				showDialogLoading();
			}
		});
	}
    //等待dialog
	private void showDialogLoading() {
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(this.LAYOUT_INFLATER_SERVICE);
		//view
		View view = inflater.inflate(R.layout.footer, null);
		final ProgressBar progressbar1 = (ProgressBar)view.findViewById(R.id.progressbar1);
		textview2 = (TextView)view.findViewById(R.id.textview2);
		AlertDialog.Builder ad = new AlertDialog.Builder(this);
		selfdialog = ad.create();
		selfdialog.setView(view);
		//屏蔽掉点击
		selfdialog.setCancelable(false);
		selfdialog.show();
		new Thread() {
			@Override
			public void run() {
				//getProgress()获得当前的进度值
				//incrementProgressBy()每次增加的量
				//getMax()获得这个进度条的范围的上限
				int max = progressbar1.getMax();
				int i = 0;
				
				while(true){
					 
					if(i > max){
						break;
					}
					try {
						Thread.sleep(1000);
						progressbar1.incrementProgressBy(1);
						Message message = new Message();
						message.what = 1;
						message.obj = progressbar1.getProgress();
						main.mhandler.sendMessage(message);
						i++;
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
				}
				//关闭dialog
				selfdialog.dismiss();
			}
		}.start();
	}
}

footer.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center" >

    <ProgressBar
        android:id="@+id/progressbar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="10"
        />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="正在下载(0/10)"
        android:textColor="@android:color/black"
        android:textSize="9sp" />

</LinearLayout>

activity_main与上个例子布局一样。



   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值