package com.dream.myapplication; import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ProgressBar;import android.widget.Spinner;import android.widget.TextView; import java.util.ArrayList;import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private ProgressBar progress; private Button add; private Button sub; private Button reset; private TextView textView; @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(888); init(); } private void init() { progress=(ProgressBar)findViewById(R.id.progressBar_horiz); add = (Button)findViewById(R.id.add); sub = (Button)findViewById(R.id.sub); reset = (Button)findViewById(R.id.reset); textView = (TextView)findViewById(R.id.text_view); int first = progress.getProgress(); int second = progress.getSecondaryProgress(); int max = progress.getMax(); textView.setText(getString(R.string.first_text) + (int) (first / (float) max * 100) + getString(R.string.second_text) + (int) (second / (float) max * 100) + getString(R.string.three_text)); add.setOnClickListener(this); sub.setOnClickListener(this); reset.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.add: { progress.incrementProgressBy(10); progress.incrementSecondaryProgressBy(10); break; } case R.id.sub: { progress.incrementProgressBy(-10); progress.incrementSecondaryProgressBy(-10); break; }case R.id.reset: { progress.setProgress(30); progress.setSecondaryProgress(50); break; } } textView.setText(getString(R.string.first_text) + (int) (progress.getProgress() / (float) progress.getMax() * 100) + getString(R.string.second_text) + (int) (progress.getSecondaryProgress() / (float) progress.getMax() * 100) +getString(R.string.three_text)); }}
时间: 2024-11-05 18:49:52