//xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="進度條" /> <progressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <progressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <progressBar style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <progressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dip" android:layout_height="wrap_content" android:id="@+id/progressBarHorizontal" android:max="100" /> <Button android:id="@+id/btnup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加進度條" android:textSize="20sp" /> <Button android:id="@+id/btndown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="減少進度條" android:textSize="20sp" />
//Java代碼
package carol.progressbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
private ProgressBar bar;
private Button btnup;
private Button btndown;
private int count=0;
private int current=50;
private String show;
private ProgressBar progressBarHorizontal;
@Override
public void onClick(View view)
{
switch (view.getId())
{
// 處理“增加進度"按鈕事件
case R.id.btnup:
count+=5;
if(count<=bar.getMax())
bar.setProgress(count);
current=bar.getProgress();
show="當前進度為: "+ String.valueOf(current);
Toast.makeText(getBaseContext(), show, Toast.LENGTH_LONG).show();
break;
// 處理“減少進度"按鈕事件
case R.id.btndown:
count-=5;
if(count<=bar.getMax())
bar.setProgress(count);
current=bar.getProgress();
show="當前進度為: "+ String.valueOf(current);
Toast.makeText(getBaseContext(), show, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
setProgressBarIndeterminateVisibility(true);
Button btnup=(Button) findViewById(R.id.btnup);
Button btndown=(Button) findViewById(R.id.btndown);
ProgressBar progressBarHorizontal=(ProgressBar) findViewById(R.id.progressBarHorizontal);
btnup.setOnClickListener(this);
btndown.setOnClickListener(this);
}
}
//logcat提醒的錯誤
12-03 02:16:52.566: E/AndroidRuntime(1496): FATAL EXCEPTION: main
12-03 02:16:52.566: E/AndroidRuntime(1496): java.lang.RuntimeException: Unable to start activity ComponentInfo{carol.progressbar/carol.progressbar.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class progressBar
12-03 02:16:52.566: E/AndroidRuntime(1496): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
12-03 02:16:52.566: E/AndroidRuntime(1496): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.progressBar" on path: DexPathList[[zip file "/data/app/carol.progressbar-1.apk"],nativeLibraryDirectories=[/data/app-lib/carol.progressbar-1, /system/lib]]
XML的progressBar是這樣的ProgressBar,第一個字母大寫