效果图:
具体实现代码:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#000000"> <TextView android:text="当前值:50" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!-- 拖动条 --> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10px" android:max="100" android:progress="50"/> </LinearLayout>
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.Toast; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class MainActivity extends Activity { private SeekBar seekbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView result=(TextView)findViewById(R.id.textView1); seekbar=(SeekBar)findViewById(R.id.seekBar1); seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { //当用户结束滑动拖动条的时候执行的方法 @Override public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(MainActivity.this,"结束滑动", Toast.LENGTH_SHORT).show(); } //当用户开始滑动拖动条的时候执行的方法 @Override public void onStartTrackingTouch(SeekBar seekBar) { Toast.makeText(MainActivity.this,"开始滑动", Toast.LENGTH_SHORT).show(); } //当用户正在滑动拖动条的时候执行的方法(参数progress就是目前拖动条的进度) @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { result.setText("当前值:"+progress); } }); } }实现效果:用户刚用手触碰上去正准备开始拖动,显示"开始滑动"提示,拖动过程"当前值:"在随着拖动进度变化,手指停止拖动时,显示"结束滑动"。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#000000"> <!-- 星级评分条 --> <RatingBar android:id="@+id/ratingBar1" android:numStars="5" android:rating="3.5" android:isIndicator="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!-- 按钮 --> <Button android:text="提交" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1"/> </LinearLayout>
MainActivity:
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RatingBar; import android.widget.Toast; public class MainActivity extends Activity { private RatingBar ratingBar;//星级评分条 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ratingBar=(RatingBar)findViewById(R.id.ratingBar1);//获取星级评分条 Button button=(Button)findViewById(R.id.button1);//获取“提交”按钮 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int result=ratingBar.getProgress();//获取进度 float rating=ratingBar.getRating();//获取等级 float step=ratingBar.getStepSize();//获取每次最少要改变多少星级 Log.i("星级评分条", "step="+step+" result="+result+" rating="+rating); Toast.makeText(MainActivity.this, "你得到了"+rating+"颗星",Toast.LENGTH_SHORT).show(); } }); } }
点击按钮之后控制台输出的信息如图:
转载请注明出处:http://blog.csdn.net/acmman/article/details/44904123