ProgressBar

来源:互联网 发布:b站知名UP主黑历史知乎 编辑:程序博客网 时间:2024/06/05 07:20
public class MainActivity extends Activity implements View.OnClickListener{    private ProgressBar  progressBar;    private Button btnAdd, btnReduce, btnReset, btnDialog;    private TextView textView;    private ProgressDialog progressDialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_PROGRESS);        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        setContentView(R.layout.activity_main);//        setProgressBarVisibility(true);//        setProgressBarIndeterminateVisibility(true);//        setProgress(600);        init();    }    private void init(){        progressBar = (ProgressBar) findViewById(R.id.horizontal);        btnAdd = (Button) findViewById(R.id.add);        btnReduce = (Button) findViewById(R.id.reduce);        btnReset = (Button) findViewById(R.id.reset);        btnDialog = (Button) findViewById(R.id.dialog);        textView = (TextView) findViewById(R.id.textView);        int first = progressBar.getProgress();        int second = progressBar.getSecondaryProgress();        int max = progressBar.getMax();        textView.setText("第一个进度是" + (int) (first/(double)max * 100) +                "%  第二个进度是" + (int) (second/(double)max * 100) + "%");        btnAdd.setOnClickListener(this);        btnReduce.setOnClickListener(this);        btnReset.setOnClickListener(this);        btnDialog.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch(v.getId()){            case R.id.add:{                progressBar.incrementProgressBy(10);                progressBar.incrementSecondaryProgressBy(10);                break;            }            case R.id.reduce:{                progressBar.incrementProgressBy(-10);                progressBar.incrementSecondaryProgressBy(-10);                break;            }            case R.id.reset: {                progressBar.setProgress(50);                progressBar.setSecondaryProgress(80);                break;            }            case R.id.dialog: {                progressDialog = new ProgressDialog(MainActivity.this);                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);                progressDialog.setTitle("Hello");                progressDialog.setMessage("Good morning!");                progressDialog.setIcon(R.mipmap.ic_launcher);                progressDialog.setMax(100);                progressDialog.incrementProgressBy(50);                //进度条明确显示按钮                progressDialog.setIndeterminate(false);                progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确认", new ProgressDialog.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "bye", Toast.LENGTH_SHORT).show();                    }                });                progressDialog.setCancelable(true);//手机返回按钮                progressDialog.show();                break;            }        }        int first = progressBar.getProgress();        int second = progressBar.getSecondaryProgress();        int max = progressBar.getMax();        textView.setText("第一个进度是" + (int) (first/(double)max * 100) +                "%  第二个进度是" + (int) (second/(double)max * 100) + "%");    }

}

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    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"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.qihao.myapplication2.MainActivity"    tools:showIn="@layout/activity_main"    android:orientation="vertical"    >    <ProgressBar        style="?android:attr/progressBarStyleLarge"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/progressBar"/>    <ProgressBar        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/progressBar2"/>    <ProgressBar        style="?android:attr/progressBarStyleSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/progressBar3"/>    <ProgressBar        style="@android:style/Widget.ProgressBar.Horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/horizontal"        android:progress="50"        android:secondaryProgress="80"        android:max="100"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/add"        android:gravity="center_horizontal"        android:id="@+id/add"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/reduce"        android:gravity="center_horizontal"        android:id="@+id/reduce"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/reset"        android:gravity="center_horizontal"        android:id="@+id/reset"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/dialog"        android:gravity="center_horizontal"        android:id="@+id/dialog"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/text"        android:id="@+id/textView"/></LinearLayout>

0 0