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="com.example.ratingbartest.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!-- 用户每次点击 增长几颗星 -->
<!-- 设置有几颗星 -->
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
/>
</RelativeLayout>
--------------------------------------------------------------
package com.example.ratingbartest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;
/*
*
* 这个类主要就是测试RatingBar 主要用于用户评级 几颗星
* 1:创建布局文件<RatingBar />
* 2:得到SeekBar对象
* 3:设置SeekBar的监听setOnRatingBarChangeListener()
* 4:创建这个监听的对象 implements RatingBar.OnRatingBarChangeListener{};
*
* */
public class MainActivity extends ActionBarActivity {
private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar=(RatingBar)findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(new RatingBarListener());
}
class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
/*
* 当用户变化选择星星的时候,该方法就会触发
* */
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
System.out.println(rating);
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}