MainActivity
public class MainActivity extends AppCompatActivity { private StarView mStarView;; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ping_jia); mStarView = (StarView) this.findViewById(R.id.star_view); //设置是否可以点击 mStarView.setCanSelected(true); // 设置显示等级 mStarView.setLevel(1); } }
————————————————————————————————————————————————————————
StarView 星星类
package com.sf.fczp.ddgl; import android.content.Context; import android.support.annotation.AttrRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import com.sf.fczp.R; import java.util.ArrayList; import java.util.List; /** * Created by kGod on 2017/3/24. * Email 18252032703@163.com * Thank you for watching my code 替换图片 TODO */ public class StarView extends FrameLayout implements View.OnClickListener { private Context mContext; private View mView; private ImageView iv1, iv2, iv3, iv4, iv5; private List<ImageView> imageViews = new ArrayList<>(); // 等级 private int level; // 是否可以点击 private boolean canSelected; public void setCanSelected(boolean isCanSelected) { this.canSelected = isCanSelected; if (canSelected) { for (ImageView iv : imageViews) { iv.setOnClickListener(this); } } } public StarView(@NonNull Context context) { this(context, null); } public StarView(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public StarView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { mContext = context; mView = View.inflate(mContext, R.layout.view_star_layout, this); iv1 = (ImageView) $(mView, R.id.iv1); iv2 = (ImageView) $(mView, R.id.iv2); iv3 = (ImageView) $(mView, R.id.iv3); iv4 = (ImageView) $(mView, R.id.iv4); iv5 = (ImageView) $(mView, R.id.iv5); imageViews.add(iv1); imageViews.add(iv2); imageViews.add(iv3); imageViews.add(iv4); imageViews.add(iv5); if (canSelected) { for (ImageView iv : imageViews) { iv.setOnClickListener(this); } } } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } @Override public void onClick(View view) { int id = view.getId(); setSeleted(id); } private void setSeleted(int id) { for (int i = 0; i < imageViews.size(); i++) { ImageView iv = imageViews.get(i); iv.setSelected(false); } switch (id) { case R.id.iv1: iv1.setSelected(true); level = 1; break; case R.id.iv2: iv1.setSelected(true); iv2.setSelected(true); level = 2; break; case R.id.iv3: iv1.setSelected(true); iv2.setSelected(true); iv3.setSelected(true); level = 3; break; case R.id.iv4: iv1.setSelected(true); iv2.setSelected(true); iv3.setSelected(true); iv4.setSelected(true); level = 4; break; case R.id.iv5: iv1.setSelected(true); iv2.setSelected(true); iv3.setSelected(true); iv4.setSelected(true); iv5.setSelected(true); level = 5; break; } } /** * 获取选择的评价等级 * * @return */ public int getLevel() { return level; } /** * 设置等级 * * @param level 可根据百分比得出12345等级用于显示 */ public void setLevel(int level) { for (int i = 0; i < imageViews.size(); i++) { ImageView iv = imageViews.get(i); iv.setSelected(false); } switch (level) { case 1: iv1.setSelected(true); break; case 2: iv1.setSelected(true); iv2.setSelected(true); break; case 3: iv1.setSelected(true); iv2.setSelected(true); iv3.setSelected(true); break; case 4: iv1.setSelected(true); iv2.setSelected(true); iv3.setSelected(true); iv4.setSelected(true); break; case 5: iv1.setSelected(true); iv2.setSelected(true); iv3.setSelected(true); iv4.setSelected(true); iv5.setSelected(true); break; } } /** * findViewById * @param view * @param id * @return */ private View $(View view, int id) { return view.findViewById(id); } }
————————————————————————————————————————————————————————
view_star_layout 星星类布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/iv1" android:layout_width="@dimen/dp_16" android:layout_height="@dimen/dp_15" android:layout_marginLeft="5dp" android:src="@drawable/selector_choose_or_no_choose"/> <ImageView android:id="@+id/iv2" android:layout_width="@dimen/dp_16" android:layout_height="@dimen/dp_15" android:layout_marginLeft="5dp" android:src="@drawable/selector_choose_or_no_choose"/> <ImageView android:id="@+id/iv3" android:layout_width="@dimen/dp_16" android:layout_height="@dimen/dp_15" android:layout_marginLeft="5dp" android:src="@drawable/selector_choose_or_no_choose"/> <ImageView android:id="@+id/iv4" android:layout_width="@dimen/dp_16" android:layout_height="@dimen/dp_15" android:layout_marginLeft="5dp" android:src="@drawable/selector_choose_or_no_choose"/> <ImageView android:id="@+id/iv5" android:layout_width="@dimen/dp_16" android:layout_height="@dimen/dp_15" android:layout_marginLeft="5dp" android:src="@drawable/selector_choose_or_no_choose"/> </LinearLayout> </LinearLayout>
————————————————————————————————————————————————————————
activity.xml
<com.sf.fczp.ddgl.StarView android:id="@+id/star_view" android:layout_width="wrap_content" android:layout_height="wrap_content"> </com.sf.fczp.ddgl.StarView>
————————————————————————————————————————————————————————
selector_choose_or_no_choose.xml drawable里添加
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/star_pressed_icon" android:state_selected="true"></item> <item android:drawable="@mipmap/star_normal_icon" android:state_selected="false"></item> </selector>