写了一个进度条的程序点击按钮出了点问题

yonghengmanlian 2014-10-31 09:08:51
刚学习安卓不久,写了一个关于进度条的小程序。功能是点击按钮后出现进度条,且进度条自动前进。不过点击了按钮后进度条出现了,但是按钮也消失了。。
代码:
MainActivity.java:
package com.example.barprogress;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
ProgressBar bar = null;
Button myButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.bar);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener());
}

class ButtonListener implements OnClickListener{

@Override
public void onClick(View arg0) {
bar.setVisibility(View.VISIBLE);
barHandler.post(updateThread);
}
}
Handler barHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
bar.setProgress(msg.arg1);
barHandler.post(updateThread);
}};

Runnable updateThread = new Runnable(){
int i = 0;
public void run(){
i = i+ 10;
System.out.println("Begin Thread");
Message msg = barHandler.obtainMessage();
msg.arg1 = i;
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
barHandler.sendMessage(msg);
if(i == 100){
barHandler.removeCallbacks(updateThread);
}
}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ProgressBar
android:id="@+id/bar"
android:layout_width="200dp"
android:layout_height="fill_parent"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start"/>



</LinearLayout>
刚开始画面:

点击按钮后,按钮消失,进度条位置也不在最上方:


...全文
325 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
svenwang 2014-11-01
  • 打赏
  • 举报
回复
引用 3 楼 yonghengmanlian 的回复:
[quote=引用 1 楼 svenwang 的回复:] 1.按钮消失是因为进度条的高度是match_parent,所以进度条显示出来的时候就把按钮遮住了。 2.你的进程模型有问题,Handler是在UI线程里创建的,执行barHandler.post(updateThread)使得updateThread会在UI线程里执行,导致updateThread里的sleep把UI线程阻塞了。 我帮你改了一下

public class MainActivity extends Activity {
	ProgressBar bar = null;
	Button myButton = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bar = (ProgressBar)findViewById(R.id.bar);
		myButton = (Button)findViewById(R.id.myButton);
		myButton.setOnClickListener(new ButtonListener());
	}
	
	class ButtonListener implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			bar.setVisibility(View.VISIBLE);
			new Thread(updateThread){}.start();
		}
	}

	Handler barHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			bar.setProgress(msg.arg1);
			new Thread(updateThread){}.start();
		}
	};
		
	Runnable updateThread = new Runnable(){
		int i = 0;
		public void run(){
			if (i < 100) {
				i += 10;
				System.out.println("Begin Thread");
				try{
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message msg = barHandler.obtainMessage();
				msg.arg1 = i;
				barHandler.sendMessage(msg);
			}
		}
	};
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  	tools:context=".MainActivity" >
   	<Button
  	    android:id="@+id/myButton"
  	    android:layout_width="match_parent"
  	    android:layout_height="wrap_content"
  	    android:text="start"/>
	<ProgressBar
  	    android:id="@+id/bar"
  	    android:layout_width="match_parent"
  	    android:layout_height="wrap_content"
  	    style="?android:attr/progressBarStyleHorizontal"
  	    android:visibility="gone"/>
</LinearLayout>
谢谢你的解答!刚来到csdn,请问如何像你那样发代码有标明行数和缩进?[/quote] 点击编辑窗口上方右数第六个按钮(带有<>标记),从下拉菜单中选择需要的语言,系统会自动在编辑窗口里生成配对的标签,你在标签里粘帖上你的代码就可以了。
yonghengmanlian 2014-11-01
  • 打赏
  • 举报
回复
引用 1 楼 svenwang 的回复:
1.按钮消失是因为进度条的高度是match_parent,所以进度条显示出来的时候就把按钮遮住了。 2.你的进程模型有问题,Handler是在UI线程里创建的,执行barHandler.post(updateThread)使得updateThread会在UI线程里执行,导致updateThread里的sleep把UI线程阻塞了。 我帮你改了一下

public class MainActivity extends Activity {
	ProgressBar bar = null;
	Button myButton = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bar = (ProgressBar)findViewById(R.id.bar);
		myButton = (Button)findViewById(R.id.myButton);
		myButton.setOnClickListener(new ButtonListener());
	}
	
	class ButtonListener implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			bar.setVisibility(View.VISIBLE);
			new Thread(updateThread){}.start();
		}
	}

	Handler barHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			bar.setProgress(msg.arg1);
			new Thread(updateThread){}.start();
		}
	};
		
	Runnable updateThread = new Runnable(){
		int i = 0;
		public void run(){
			if (i < 100) {
				i += 10;
				System.out.println("Begin Thread");
				try{
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message msg = barHandler.obtainMessage();
				msg.arg1 = i;
				barHandler.sendMessage(msg);
			}
		}
	};
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  	tools:context=".MainActivity" >
   	<Button
  	    android:id="@+id/myButton"
  	    android:layout_width="match_parent"
  	    android:layout_height="wrap_content"
  	    android:text="start"/>
	<ProgressBar
  	    android:id="@+id/bar"
  	    android:layout_width="match_parent"
  	    android:layout_height="wrap_content"
  	    style="?android:attr/progressBarStyleHorizontal"
  	    android:visibility="gone"/>
</LinearLayout>
谢谢你的解答!刚来到csdn,请问如何像你那样发代码有标明行数和缩进?
svenwang 2014-10-31
  • 打赏
  • 举报
回复
其实这样效率上不会太高,因为进度条每次更新时都创建一个线程。你可以只创建一个后台线程,在一个loop里做消息处理。 改进以后的程序是这样的:

public class MainActivity extends Activity {
	ProgressBar bar = null;
	Button myButton = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bar = (ProgressBar)findViewById(R.id.bar);
		myButton = (Button)findViewById(R.id.myButton);
		myButton.setOnClickListener(new ButtonListener());
	}
	
	class ButtonListener implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			bar.setVisibility(View.VISIBLE);
			new Thread(updateThread){}.start();
		}
	}

	Handler barHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			bar.setProgress(msg.arg1);
		}
	};
		
	Runnable updateThread = new Runnable(){
		public void run(){
			System.out.println("Begin Thread");
			for (int i = 0; i <= 100; i += 10) {
				try{
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message msg = barHandler.obtainMessage();
				msg.arg1 = i;
				barHandler.sendMessage(msg);
			}
		}
	};
}
svenwang 2014-10-31
  • 打赏
  • 举报
回复
1.按钮消失是因为进度条的高度是match_parent,所以进度条显示出来的时候就把按钮遮住了。 2.你的进程模型有问题,Handler是在UI线程里创建的,执行barHandler.post(updateThread)使得updateThread会在UI线程里执行,导致updateThread里的sleep把UI线程阻塞了。 我帮你改了一下

public class MainActivity extends Activity {
	ProgressBar bar = null;
	Button myButton = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bar = (ProgressBar)findViewById(R.id.bar);
		myButton = (Button)findViewById(R.id.myButton);
		myButton.setOnClickListener(new ButtonListener());
	}
	
	class ButtonListener implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			bar.setVisibility(View.VISIBLE);
			new Thread(updateThread){}.start();
		}
	}

	Handler barHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			bar.setProgress(msg.arg1);
			new Thread(updateThread){}.start();
		}
	};
		
	Runnable updateThread = new Runnable(){
		int i = 0;
		public void run(){
			if (i < 100) {
				i += 10;
				System.out.println("Begin Thread");
				try{
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message msg = barHandler.obtainMessage();
				msg.arg1 = i;
				barHandler.sendMessage(msg);
			}
		}
	};
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  	tools:context=".MainActivity" >
   	<Button
  	    android:id="@+id/myButton"
  	    android:layout_width="match_parent"
  	    android:layout_height="wrap_content"
  	    android:text="start"/>
	<ProgressBar
  	    android:id="@+id/bar"
  	    android:layout_width="match_parent"
  	    android:layout_height="wrap_content"
  	    style="?android:attr/progressBarStyleHorizontal"
  	    android:visibility="gone"/>
</LinearLayout>

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧