源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android开发之拖动条和评分组件用法分析

  • 时间:2021-10-11 17:56 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android开发之拖动条和评分组件用法分析
本文实例讲述了Android开发之拖动条和评分组件用法。分享给大家供大家参考,具体如下: 今天闲着没事做就拿出了Android书接着学习,android就是组件多有时候还会弄混淆了。这次介绍的是拖动条和评分组件,这2个组件也是超级简单的 下面就一个一个的来研究。 1. 拖动条,就类似android手机上调节音量那个,该组件和对话框不同的是用户可以操作。该组件的xml表现形式如下
<SeekBar
   android:id="@+id/seek"
   android:layout_marginLeft="20dip"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>

光这样太枯燥了,这里我做了个拖动切换图片的小例子。这里就要介绍下拖动条的长度了这个长度是可控制的像这里我有5张图片拖动条的size就是5.这个设置我直接写在了程序里。实现很简单就是在拖动条的基础上加个ImageView,然后对拖动条进行拖动的监听值改变了就切换ImageView就好了。下面是主要代码。
bar=(SeekBar)this.findViewById(R.id.seek);
imageview=(ImageView)this.findViewById(R.id.img);
bar.setMax(5);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
  }
  public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
  }
  public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
    //切换图片
    imageview.setImageResource(imagedata[seekBar.getProgress()]);
  }
});

效果图如下所示,我觉得还是挺好的。 [img]http://files.jb51.net/file_images/article/201707/2017718100934392.png?201761810105[/img] 2. 下面就是评分插件了,android默认情况下是五角星,如果有必要可以弄成别的 这里我弄了一种自定义的类型,来看下吧 (1)在drawable下面弄个xml文件内容如下
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+android:id/background"
    android:drawable="@drawable/star_empty"
 />
  <item android:id="@+android:id/secondaryProgress"
    android:drawable="@drawable/star_empty"
 />
  <item android:id="@+android:id/progress"
    android:drawable="@drawable/star_full"
 />
</layer-list>

star_empty是灰色的五角星,star_full是黄色的五角星。上面的配置文件理解起来也很简单,下面就把这个样式嵌入到评分组件上,首先还是写个样式文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style
  name="ratingbarstyle"
  parent="@android:style/Widget.RatingBar">
  <item name="android:progressDrawable">
    @drawable/starconfig
  </item>
  <item name="android:minHeight">53dip</item>
   <item name="android:maxHeight">53dip</item>
 </style>
</resources>

顺便说明下starconfig就是上面那个配置文件,下面就是标签了,该组件的常用属性是步长、星星的个数就像下面这样
<RatingBar
    android:id="@+id/rating"
    android:numStars="5"
    android:stepSize="1"
    style="@style/ratingbarstyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

后面也自然是事件处理了
rbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
  public void onRatingChanged(RatingBar ratingBar, float rating,
      boolean fromUser) {
    int num=(int)rating;
    String result=null;
    switch (num) {
    case 1:
      result="非常不满意";
      break;
    case 2:
      result="不满意";
      break;
    case 3:
      result="还可以";
      break;
    case 4:
      result="满意";
      break;
    case 5:
      result="非常满意";
      break;
    default:
      break;
    }
    Toast.makeText(MyDailogActivity.this, result, 1).show();
  }
});

来看下效果吧 [img]http://files.jb51.net/file_images/article/201707/2017718101006724.png?2017618101120[/img] 更多关于Android相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/381.htm]Android基本组件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/371.htm]Android布局layout技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部