当前位置: 代码迷 >> Android >> 写了一个进度条的程序点击按钮出了点有关问题
  详细解决方案

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

热度:47   发布时间:2016-04-28 03:47:14.0
写了一个进度条的程序点击按钮出了点问题
    刚学习安卓不久,写了一个关于进度条的小程序。功能是点击按钮后出现进度条,且进度条自动前进。不过点击了按钮后进度条出现了,但是按钮也消失了。。
代码:
     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>
刚开始画面:

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


    
------解决思路----------------------
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>

------解决思路----------------------
其实这样效率上不会太高,因为进度条每次更新时都创建一个线程。你可以只创建一个后台线程,在一个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);
}
}
};
}

------解决思路----------------------
引用:
Quote: 引用:

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,请问如何像你那样发代码有标明行数和缩进?

点击编辑窗口上方右数第六个按钮(带有<>标记),从下拉菜单中选择需要的语言,系统会自动在编辑窗口里生成配对的标签,你在标签里粘帖上你的代码就可以了。
  相关解决方案