手动增减进度条
慕課網學到的。設置幾個按鈕手動更新進度條,理解一下下吧。
直接接著簡單進度條的代碼就不刪了。先上圖
圖是不是大了點。。。算了。。。
去先設置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" /><!-- 中環不設置 --><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>中間有一坨不必要的別的進度條的懶得刪了,,,
然后設置主文件。
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);//啟用窗口特征,啟用帶進度和不帶進度的 requestWindowFeature(Window.FEATURE_PROGRESS);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.main);//顯示兩種進度條setProgressBarVisibility(true);setProgressBarIndeterminateVisibility(false);setProgress(9999);//直進度條進度,最大量為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()獲取第一進度條int second=progress.getSecondaryProgress();//獲取第二進度條int max = progress.getMax();//獲取最大進度 text.setText("第一進度百分比"+(int)(frist/(float)max*100)+"% 第二進度百分比"+(int)(second/(float)max*100)+"%");//進度提示 add.setOnClickListener(this);//設置監控器reduce.setOnClickListener(this);reset.setOnClickListener(this);}@Override//監聽事件邏輯public void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.add:{progress.incrementProgressBy(10);//增加第一進度十個progress.incrementSecondaryProgressBy(10);//增加第二進度十個break;}case R.id.reduce:{progress.incrementProgressBy(-10);//減少第一進度十個progress.incrementSecondaryProgressBy(-10);//減少第二進度十個break;}case R.id.reset:{progress.setProgress(10);progress.setSecondaryProgress(20);break;}}//每次點擊完成動態更新text.setText("第一進度百分比"+(int)(progress.getProgress()/(float)progress.getMax()*100)+"% 第二進度百分比"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%");};}同理有三個進度條的是額外的。
然后點擊add,reduce玩♂弄進度條吧
轉載于:https://www.cnblogs.com/webgavin/p/5720887.html
總結
- 上一篇: linux关机正确方法
- 下一篇: 解决centos ping不通外网