这是mainActivity.java
package main.i_love_luoxiao_05;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;public class MainActivity extends Activity {
private ProgressBar bar;
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.progressBar);
startButton = (Button)findViewById(R.id.myButton);
startButton.setOnClickListener(new ButtonListener());
} class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
bar.setVisibility(View.VISIBLE);
handler.post(update);
}
}
Handler handler = new Handler();
public void handleMessage(Message msg){
bar.setProgress(msg.arg1);
handler.post(update);
}
Runnable update = new Runnable() {
int i = 0;
@Override
public void run() {
// TODO Auto-generated method stub
i = i+10;
Message msg =handler.obtainMessage();
msg.arg1 = i;
try{
Thread.sleep(1000);
}catch (InterruptedException o){
o.printStackTrace();
}
handler.sendMessage(msg);
if(i==100)
handler.removeCallbacks(update);
}
};
}
这个是xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ProgressBar
android:id="@+id/progressBar"
android:layout_width="200dp"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
android:max="100"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/progressBar"/></RelativeLayout>
package main.i_love_luoxiao_05;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;public class MainActivity extends Activity {
private ProgressBar bar;
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.progressBar);
startButton = (Button)findViewById(R.id.myButton);
startButton.setOnClickListener(new ButtonListener());
} class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
bar.setVisibility(View.VISIBLE);
handler.post(update);
}
}
Handler handler = new Handler();
public void handleMessage(Message msg){
bar.setProgress(msg.arg1);
handler.post(update);
}
Runnable update = new Runnable() {
int i = 0;
@Override
public void run() {
// TODO Auto-generated method stub
i = i+10;
Message msg =handler.obtainMessage();
msg.arg1 = i;
try{
Thread.sleep(1000);
}catch (InterruptedException o){
o.printStackTrace();
}
handler.sendMessage(msg);
if(i==100)
handler.removeCallbacks(update);
}
};
}
这个是xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ProgressBar
android:id="@+id/progressBar"
android:layout_width="200dp"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
android:max="100"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/progressBar"/></RelativeLayout>
解决方案 »
- 真机做TCP Server,请问IP地址是多少?
- android的演示程序
- android的几个问题
- 关于Android ffmpeg 的问题??请教各位前辈!!!!!!!!!!!
- android 如何访问网络路径问题!
- android百度地图
- 内部静态类是作什么用的?
- Android 闹钟启动的广播如何注销?
- 请问OGL ES存放顶点数据应使用Handler还是Bindbuffer?
- Android之Error: 'L' is not a valid file-based resource name character如何解决
- 请问,谁用过speex/ iLBC/ Silk语音编码,稳定性怎么样,编码解码后的语音质量,怎么样,谢谢,顶着有分
- 地图开发里面的ontap方法
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;public class MainActivity extends Activity {
private ProgressBar bar;
private Button startButton;
Handler handler = new Handler(){ @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
bar.setProgress(msg.arg1);
handler.post(update);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar) findViewById(R.id.progressBar);
startButton = (Button) findViewById(R.id.myButton);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bar.setVisibility(View.VISIBLE);
handler.post(update);
}
});
} Runnable update = new Runnable() { int i = 0; @Override
public void run() {
// TODO Auto-generated method stub
i = i + 10;
Message msg = handler.obtainMessage();
msg.arg1 = i;
try {
Thread.sleep(1000);
} catch (InterruptedException o) {
o.printStackTrace();
}
handler.sendMessage(msg);
if (i == 100)
handler.removeCallbacks(update);
}
};}不知道这样符合不符合你的要求,改了一下你的代码!