anroid 学习笔记18 ------ SeekBar 和 RatingBar
废话不讲,上代码
布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > /*可选择进度条*/ <SeekBar android:id="@+id/two" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="500" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/two" android:textColor="#ff0000" /> /*星星进度条*/ <RatingBar android:id="@+id/xingxing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text" android:numStars="5" android:stepSize="0.5" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/xingxing" android:textColor="#ff0000" /> </RelativeLayout>
java 代码
package com.lihao.bar; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.RatingBar; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class MainActivity extends Activity { private SeekBar two; private TextView text; private RatingBar xingxing; private TextView text2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView)findViewById(R.id.text); text2 = (TextView)findViewById(R.id.text2); two = (SeekBar)findViewById(R.id.two); xingxing = (RatingBar)findViewById(R.id.xingxing); /*设置参数 绑定监听器*/ two.setProgress(100); two.setSecondaryProgress(150); Barlistener listener = new Barlistener(); two.setOnSeekBarChangeListener(listener); /*给xingxing绑定监听器*/ Ratlistener rlistener = new Ratlistener(); xingxing.setOnRatingBarChangeListener(rlistener); } /** * * @author Administrator *arg0 RatingBar对象 *arg1 选中的星星的个数 *arg2 是否是人为操作 */ class Ratlistener implements OnRatingBarChangeListener{ @Override public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) { // TODO Auto-generated method stub if(arg2) { text2.setText("我选了"+arg1+"颗星" ); } } } /** * * @author Administrator *arg0 SeekBar 对象 *arg1 进度条的长度 *arg2 是否人为操作 */ class Barlistener implements OnSeekBarChangeListener{ @Override public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { // TODO Auto-generated method stub if(arg2) { text.setText("我的长度是:"+arg0.getProgress()); } } @Override public void onStartTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub text.setText("帅哥你触摸了我"); } @Override public void onStopTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
贴图