Android開發——ProgressBar進度條進度控制
阿新 • • 發佈:2018-12-30
今天花了很多時間給新電腦配置環境,主要是之前的eclipse版本有問題導致配置好所有的開發環境後不能新建專案,一建專案就卡了軟體,這是個尷尬的問題。最開始我還以為是sdk或者adt出了問題,後來發現並不是,換了一遍一樣出錯,然後換了一個eclipse,終於可以開始快樂的學習了。最後main尷尬的發現新電腦上genymotion又GG了,虛擬機器問題,考慮到今天學習的內容比較多,這個問題先不去解決,畢竟咱窮人用的是android手機,大不了用真機嘛,so easy 的事情,明天解決虛擬機器問題。今天就直接貼程式碼了,有些新的或者說比較難的東西咱就說一說,其他的就直接看程式碼吧。
好了,今天我們先來一個ProgressBar進度條控制元件。
activity_main.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="com.example.progressbar.MainActivity" >
<ProgressBar
android:id="@+id/pb"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/pb"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bt1"
android:text="增加" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bt2"
android:text="減小" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.progressbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.pb);
progressBar.setMax(100);
}
private int progress = 0;//進度
public void bt1 (View view) {
progress += 5;
if (progress >= 100) {
progress = 100;
}
progressBar.setProgress(progress);
}
public void bt2(View view) {
progress -= 5;
if (progress <= 0) {
progress = 0;
}
progressBar.setProgress(progress);
}
}
注意我紅色標出的地方哦,我們換了一種方式來寫它的事件。