手动增减进度条
慕課網(wǎng)學(xué)到的。設(shè)置幾個(gè)按鈕手動(dòng)更新進(jìn)度條,理解一下下吧。
直接接著簡單進(jìn)度條的代碼就不刪了。先上圖
圖是不是大了點(diǎn)。。。算了。。。
去先設(shè)置main.xml文件。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ProgressBarandroid:id="@+id/progressBar2"style="?android:attr/progressBarStyleLarge"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ProgressBarandroid:id="@+id/progressBar1"android:layout_width="wrap_content"android:layout_height="wrap_content" /><!-- 中環(huán)不設(shè)置 --><ProgressBarandroid:id="@+id/progressBar3"style="?android:attr/progressBarStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ProgressBarandroid:secondaryProgress="20"android:progress="10"android:max="100"android:id="@+id/horiz"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="add" /><Buttonandroid:id="@+id/reduce"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="reduce" /><Buttonandroid:id="@+id/reset"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="reset" /><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /></LinearLayout>中間有一坨不必要的別的進(jìn)度條的懶得刪了,,,
然后設(shè)置主文件。
package com.example.deemo;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {private ProgressBar progress;private Button add;private Button reduce;private Button reset;private TextView text;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//啟用窗口特征,啟用帶進(jìn)度和不帶進(jìn)度的 requestWindowFeature(Window.FEATURE_PROGRESS);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.main);//顯示兩種進(jìn)度條setProgressBarVisibility(true);setProgressBarIndeterminateVisibility(false);setProgress(9999);//直進(jìn)度條進(jìn)度,最大量為10000 init();}private void init() {progress=(ProgressBar) findViewById(R.id.horiz);add=(Button) findViewById(R.id.add);reduce=(Button) findViewById(R.id.reduce);reset=(Button) findViewById(R.id.reset);text=(TextView) findViewById(R.id.text);int frist = progress.getProgress();//getProgress()獲取第一進(jìn)度條int second=progress.getSecondaryProgress();//獲取第二進(jìn)度條int max = progress.getMax();//獲取最大進(jìn)度 text.setText("第一進(jìn)度百分比"+(int)(frist/(float)max*100)+"% 第二進(jìn)度百分比"+(int)(second/(float)max*100)+"%");//進(jìn)度提示 add.setOnClickListener(this);//設(shè)置監(jiān)控器reduce.setOnClickListener(this);reset.setOnClickListener(this);}@Override//監(jiān)聽事件邏輯public void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.add:{progress.incrementProgressBy(10);//增加第一進(jìn)度十個(gè)progress.incrementSecondaryProgressBy(10);//增加第二進(jìn)度十個(gè)break;}case R.id.reduce:{progress.incrementProgressBy(-10);//減少第一進(jìn)度十個(gè)progress.incrementSecondaryProgressBy(-10);//減少第二進(jìn)度十個(gè)break;}case R.id.reset:{progress.setProgress(10);progress.setSecondaryProgress(20);break;}}//每次點(diǎn)擊完成動(dòng)態(tài)更新text.setText("第一進(jìn)度百分比"+(int)(progress.getProgress()/(float)progress.getMax()*100)+"% 第二進(jìn)度百分比"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%");};}同理有三個(gè)進(jìn)度條的是額外的。
然后點(diǎn)擊add,reduce玩♂弄進(jìn)度條吧
轉(zhuǎn)載于:https://www.cnblogs.com/webgavin/p/5720887.html
總結(jié)
- 上一篇: linux关机正确方法
- 下一篇: javascript的null 和und