marsAndroid学习——SeekBar和RatingBar控件
笔记摘要:
本次主要对于SeekBar和RatingBar的基本创建做了说明。都非常简单,在布局文件中创建一个Bar,然后在Activity代码中实现并绑定相应的监听器,
实现必要的方法即可,是比较简单,以后和其他控件一起使用就可实现更好的效果,既然学习了,就写下来,虽然简单,但烂笔头总低得过好记性,
以后使用时直接拿来就行。
SeekBar:
SeekBar效果图
代码体现
布局代码:
<SeekBar android:id="@+id/seekBarId" android:layout_width="fill_parent" android:layout_height="wrap_content" />
SeekBarActivity代码:
package com.example.seekbar; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.SeekBar; public class SeekBarActivity extends Activity { private SeekBar seekBar = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.seek_bar); seekBar = (SeekBar) findViewById(R.id.seekBarId); seekBar.setOnSeekBarChangeListener(new SeekBarListener()); } private final class SeekBarListener implements SeekBar.OnSeekBarChangeListener{ //进度条发生改变的 时候,调用该方法,可以通过boolean :fromUser来判断是否时用户滑动进度条 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { System.out.println(progress); } //当用户滑动滑块时,调用该方法 public void onStartTrackingTouch(SeekBar seekBar) { System.out.println("start--------->"+seekBar.getProgress()); } //当用户停止滑动式,调用该方法 public void onStopTrackingTouch(SeekBar seekBar) { System.out.println("stop--------->"+seekBar.getProgress()); }} }
RatingBar
RatingBar效果图:
代码体现
RatingBar布局代码
<RatingBar android:id="@+id/ratingBarId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="1.0" />
RatingBarActivity代码
package com.example.ratingbar; import android.app.Activity; import android.os.Bundle; import android.widget.RatingBar; public class MainActivity extends Activity { private RatingBar ratingBar = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ratingBar = (RatingBar) findViewById(R.id.ratingBarId); ratingBar.setOnRatingBarChangeListener(new RatingBarListener()); } private final class RatingBarListener implements RatingBar.OnRatingBarChangeListener{ public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { System.out.println("rating-------->"+rating); }} }
以上学习来自marsAndroid的视频与论坛:http://www.mars-droid.com/bbs/forum.php