zoukankan      html  css  js  c++  java
  • ProgressBar、RatingBar和Spinner控件

    1、ProgressBar、SeekBar与RatingBar控件

    ProgressBar控件,也就是我们通常的进度条控件,可以显示加载的进度等。SeekBar控件,滑块控件,可以根据用户的需要动态为赋值。本例子就是拖动SeekBar,让进度条实时显示拖动的进度。RatingBar控件,星条控件,我们经常见到的打分控件,五颗星星。下面是main.xml页面的代码。我们声明了几个控件。

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <TextView
    12         android:id="@+id/textView1"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="@string/hello_world" />
    16 
    17     <ProgressBar
    18         android:id="@+id/progressBar1"
    19         android:layout_width="fill_parent"
    20         android:layout_height="wrap_content"
    21         android:layout_alignParentLeft="true"
    22         android:layout_below="@+id/textView1"
    23         android:layout_marginTop="68dp"
    24         android:background="@drawable/color" />
    25 
    26     <SeekBar
    27         android:id="@+id/seekBar1"
    28         android:layout_width="match_parent"
    29         android:layout_height="wrap_content"
    30         android:layout_alignParentLeft="true"
    31         android:layout_alignParentRight="true"
    32         android:layout_below="@+id/progressBar1"
    33         android:layout_marginTop="48dp" />
    34 
    35     <RatingBar
    36         android:id="@+id/ratingBar1"
    37         android:layout_width="wrap_content"
    38         android:layout_height="wrap_content"
    39         android:layout_alignLeft="@+id/seekBar1"
    40         android:layout_below="@+id/seekBar1"
    41         android:layout_marginTop="68dp" />
    42 
    43     <ProgressBar
    44         android:id="@+id/progressBar2"
    45         style="?android:attr/progressBarStyleHorizontal"
    46         android:layout_width="fill_parent"
    47         android:layout_height="wrap_content"
    48         android:layout_alignParentRight="true"
    49         android:layout_below="@+id/ratingBar1" />
    50 
    51 </RelativeLayout>
    控件生成

    这里我们只需要捕捉seekbar的拖动事件以及RatingBar控件的事件。SeekBar的事件是setOnSeekBarChangeListener,而Ratingbar的事件为setOnRatingBarChangeListener。

     1     protected void onCreate(Bundle savedInstanceState) {
     2         super.onCreate(savedInstanceState);
     3         setContentView(R.layout.activity_main);
     4         final SeekBar seek=(SeekBar)findViewById(R.id.seekBar1);
     5         final ProgressBar process=(ProgressBar)findViewById(R.id.progressBar2);
     6         final RatingBar rating=(RatingBar)findViewById(R.id.ratingBar1);
     7         rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
     8             
     9             @Override
    10             public void onRatingChanged(RatingBar ratingBar, float rating,
    11                     boolean fromUser) {
    12                 // TODO 自动生成的方法存根
    13                 float rate=ratingBar.getRating();
    14                 process.setProgress((int)(rate/5*100));
    15             }
    16         });
    17     
    18         seek.setOnSeekBarChangeListener(new  SeekBar.OnSeekBarChangeListener() {
    19             
    20             @Override
    21             public void onStopTrackingTouch(SeekBar seekBar) {
    22                 // TODO 自动生成的方法存根
    23                 process.setProgress(seek.getProgress());
    24             }
    25             
    26             @Override
    27             public void onStartTrackingTouch(SeekBar seekBar) {
    28                 // TODO 自动生成的方法存根
    29                 
    30             }
    31             
    32             @Override
    33             public void onProgressChanged(SeekBar seekBar, int progress,
    34                     boolean fromUser) {
    35                 // TODO 自动生成的方法存根
    36                 
    37             }
    38         });
    39     }
    事件监听

    这样我们就对进度条的控件有了初步了解。

    2.Spinner控件

    Spinner控件,下拉列表控件,和ComBox控件有点相似,使用的时候,也需要为他制定item。通过在String.xml

    文件中添加新的字符串资源。在后台通过适配器将资源绑定给spinner控件。

     1     BaseAdapter baseA=new BaseAdapter(){
     2 
     3                 @Override
     4                 public int getCount() {
     5                     // TODO 自动生成的方法存根
     6                     return strid.length;
     7                 }
     8 
     9                 @Override
    10                 public Object getItem(int position) {
    11                     // TODO 自动生成的方法存根
    12                     return null;
    13                 }
    14 
    15                 @Override
    16                 public long getItemId(int position) {
    17                     // TODO 自动生成的方法存根
    18                     return 0;
    19                 }
    20 
    21                 @Override
    22                 public View getView(int position, View convertView,
    23                         ViewGroup parent) {
    24                     // TODO 自动生成的方法存根
    25                     LinearLayout li=new LinearLayout(MainActivity.this);
    26                     TextView text1=new TextView(MainActivity.this);
    27                     text1.setText(" "+getResources().getText(strid[position]));
    28                     text1.setTextSize(24);
    29                 li.addView(text1);
    30                 return li;
    31                 }};
    32                 spinner.setAdapter(baseA);
    适配器绑定
  • 相关阅读:
    数据库:常用的类库、对应的方法和属性
    robotframe常用的类库、对应的方法和属性
    appium常用的类库、对应的方法和属性
    selenium常用的类库、对应的方法和属性
    Python常用的类库、对应的方法和属性
    Python MySQLdb中执行SQL语句传入的参数应该要加上引号如果该字段是str类型的
    接口测试,如何构建json类型的参数值
    使用pip install mysqlclient命令安装mysqlclient失败?(基于Python)
    如何切换虚拟机(centos6)和windows
    ZOJ
  • 原文地址:https://www.cnblogs.com/ggz19/p/3818577.html
Copyright © 2011-2022 走看看