进度条和seekbar控件的实习----安卓笔记

练习要求:

a)  新增一个页面,如下图所示,首先显示Small、Large、Inverse、Horizontal

        等四种方式的进度条。然后在下面添加“增加”,“减少”,“可视”等三

        个按钮。最后一行显示一个SeekBar 。 

    b)  针对 Small  样式的进度条:点击“可视”按钮,如果当前是可视的,则

        让其消失。否则,让其可视。 

    c)  针对 Horizontal  样式的进度条:点击增加或者减少,让其位置每次增加

        或者减少一部分(每次增减的量自己设置)。 

    d)  针对SeekBar,当用户滑动时,在SeekBar 下方利用文字“当前位置XXX ”

        显示当前滑动到的位置。如果用户停止滑动,则显示“拖动滑块设置”

        文字。 

 

 

运行后的主界面

 

点击VisibleSmall按钮可以决定Progressbar.Small是否看见

.java主程序

  1 package com.example.seekbar;
  2 
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.view.View;
  6 import android.view.View.OnClickListener;
  7 import android.widget.Button;
  8 import android.widget.ProgressBar;
  9 import android.widget.SeekBar;
 10 import android.widget.SeekBar.OnSeekBarChangeListener;
 11 import android.widget.TextView;
 12  
 13  public class MainActivity extends Activity {
 14  
 15      private Button btnAdd, btnReduce, btnVisible;
 16      private ProgressBar pbHor, pbSmall;
 17      private TextView textview1, textview2;
 18      private SeekBar seekbar;
 19  
 20      @Override
 21      protected void onCreate(Bundle savedInstanceState) {
 22          super.onCreate(savedInstanceState);
 23          setContentView(R.layout.activity_main);
 24 
 25 
 26          btnAdd = (Button) findViewById(R.id.btnAdd);
 27          btnReduce = (Button) findViewById(R.id.btnReduce);
 28          btnVisible = (Button) findViewById(R.id.btnVisible);
 29          pbHor = (ProgressBar) findViewById(R.id.pbHor);
 30          pbSmall = (ProgressBar) findViewById(R.id.pbSmall);
 31          textview1 = (TextView) findViewById(R.id.textview1);
 32          textview2 = (TextView) findViewById(R.id.textview2);
 33          seekbar = (SeekBar) findViewById(R.id.seekbar);
 34 
 35  
 36          btnAdd.setOnClickListener(mathClick);
 37          btnReduce.setOnClickListener(mathClick);
 38          seekbar.setOnSeekBarChangeListener(seekBarChange);
 39          
 40          btnVisible.setOnClickListener(new View.OnClickListener() {
 41  
 42              @Override
 43              public void onClick(View v) {
 44                  // 判断Small进度条是否显示,显示则隐藏,隐藏则显示
 45                  if (pbSmall.getVisibility() == View.VISIBLE) {
 46                      pbSmall.setVisibility(View.GONE);
 47                  } else {
 48                      pbSmall.setVisibility(View.VISIBLE);
 49                  }
 50  
 51              }
 52          });
 53      }
 54  
 55 private View.OnClickListener mathClick = new OnClickListener() {
 56  
 57          @Override
 58          public void onClick(View v) {
 59              switch (v.getId()) {
 60              case R.id.btnAdd:
 61 
 62                  if (pbHor.getProgress() < 90) {
 63                      pbHor.setProgress((int) (pbHor.getProgress() * 1.2));
 64                  }
 65                  if (pbHor.getSecondaryProgress() < 100) {
 66                      pbHor.setSecondaryProgress((int) (pbHor
 67                              .getSecondaryProgress() * 1.2));
 68                  }
 69                  break;
 70              case R.id.btnReduce:
 71              
 72                  if (pbHor.getProgress() > 10) {
 73                      pbHor.incrementProgressBy(-10);
 74                  }
 75                  if (pbHor.getSecondaryProgress() > 20) {
 76                      pbHor.incrementSecondaryProgressBy(-10);
 77                  }
 78                  break;
 79              }
 80          }
 81      };
 82     
 83      
 84  private OnSeekBarChangeListener seekBarChange = new OnSeekBarChangeListener() {
 85 
 86           @Override
 87           public void onStopTrackingTouch(SeekBar seekBar) {
 88 
 89                    if (seekBar.getId() == R.id.seekbar) {
 90 
 91                   textview1.setText("拖动滑块设置");
 92 
 93                    } 
 94 
 95             }
 96          
 97           @Override
 98           public void onStartTrackingTouch(SeekBar seekBar) {
 99 
100 
101                  }
102 
103            @Override
104            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
105            {
106 
107                if (seekBar.getId() == R.id.seekbar) {
108 
109                      textview2.setText("当前位置是:" + progress);
110                 } 
111            }
112 
113      };   
114 
115  }


main.xml布局  (注:只有Widget.ProgressBar.Horizontal风格的进度条,才可以设置进度的递增

  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2       android:layout_width="match_parent"
  3       android:layout_height="wrap_content"
  4       android:orientation="vertical" > <!--垂直布局-->
  5      
  6   
  7       <TextView
  8          android:layout_width="wrap_content"
  9          android:layout_height="wrap_content"
 10          android:text="Widget.ProgressBar.Small" />
 11  
 12      <ProgressBar
 13          android:id="@+id/pbSmall"
 14          style="@android:style/Widget.ProgressBar.Small"
 15          android:layout_width="wrap_content"
 16          android:layout_height="wrap_content" 
 17          android:layout_gravity="center_horizontal"/>
 18  
 19      <TextView
 20          android:layout_width="wrap_content"
 21          android:layout_height="wrap_content"
 22          android:text="Widget.ProgressBar.Large" />
 23  
 24      <ProgressBar
 25          style="@android:style/Widget.ProgressBar.Large"
 26          android:layout_width="wrap_content"
 27          android:layout_height="wrap_content"
 28          android:layout_gravity="center_horizontal" />
 29  
 30     <TextView
 31          android:layout_width="wrap_content"
 32          android:layout_height="wrap_content"
 33          android:text="Widget.ProgressBar.Inverse" />
 34  
 35      <ProgressBar
 36          style="@android:style/Widget.ProgressBar.Inverse"
 37          android:layout_width="wrap_content"
 38          android:layout_height="wrap_content" 
 39          android:layout_gravity="center_horizontal"/>
 40  
 41     <TextView
 42          android:layout_width="wrap_content"
 43          android:layout_height="wrap_content"
 44          android:text="Widget.ProgressBar.Horizontal" />
 45  
 46      <ProgressBar
 47          android:id="@+id/pbHor"
 48          style="@android:style/Widget.ProgressBar.Horizontal"
 49          android:layout_width="match_parent"
 50          android:layout_height="wrap_content"
 51          android:max="100"
 52          android:progress="20"
 53          android:secondaryProgress="60" />
 54      <LinearLayout
 55          android:layout_width="wrap_content"
 56          android:layout_height="wrap_content"
 57          android:orientation="horizontal" > 
 58          <!-- 水平布局-->
 59          <!-- 设置一个按钮控制水平进度的递增 -->
 60     <Button
 61          android:id="@+id/btnAdd"
 62          android:layout_width="wrap_content"
 63          android:layout_height="wrap_content"
 64          android:text="  +  " />
 65          <!-- 设置一个按钮控制水平进度的递减 -->
 66     <Button
 67          android:id="@+id/btnReduce"
 68          android:layout_width="wrap_content"
 69          android:layout_height="wrap_content"
 70          android:layout_marginLeft="30dp"
 71          android:text="  -  " />
 72          <!-- 设置一个按钮控制Style为small的进度显示与隐藏 -->
 73     <Button
 74          android:id="@+id/btnVisible"
 75          android:layout_width="wrap_content"
 76          android:layout_height="wrap_content"
 77          android:layout_marginLeft="30dp"
 78          android:text="VisibleSmall" />
 79          
 80      </LinearLayout>
 81      
 82       <SeekBar       
 83          android:layout_marginTop="30dp"
 84          android:id="@+id/seekbar"
 85          android:layout_width="match_parent"
 86          android:layout_height="wrap_content"
 87          android:max="100"
 88          android:progress="30" />
 89       
 90          <!-- 设置一个拖动条 -->
 91        <TextView
 92            android:id="@+id/textview1"
 93          android:layout_width="wrap_content" 
 94          android:layout_height="wrap_content" 
 95          android:gravity="center_horizontal"/>
 96       
 97     <TextView
 98            android:id="@+id/textview2"
 99          android:layout_width="wrap_content" 
100          android:layout_height="wrap_content" 
101          android:gravity="center_horizontal"/>
102      
103     
104  </LinearLayout>

 

posted @ 2014-04-12 20:05  iseeview  阅读(1420)  评论(0编辑  收藏  举报