android之RatingBar控件用法详解
程序员文章站
2024-03-04 20:57:30
mainactivity.java
package com.example.mars_2500_ratingbar;
import android.supp...
mainactivity.java
package com.example.mars_2500_ratingbar; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.app.activity; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.button; import android.widget.ratingbar; import android.widget.ratingbar.onratingbarchangelistener; import android.os.build; public class mainactivity extends activity { ratingbar ratingbar; button button1; public static string tag="mainactivity"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ratingbar=(ratingbar)findviewbyid(r.id.ratingbar1); ratingbar.setonratingbarchangelistener(new ratingbarchangelistener()); button1=(button)findviewbyid(r.id.buttn1); button1.setonclicklistener(new clicklistener()); } class ratingbarchangelistener implements onratingbarchangelistener { @override public void onratingchanged(ratingbar ratingbar, float rating, boolean fromuser) { log.i(tag, "当前分数="+rating); system.out.println("当前分数="+rating); } } class clicklistener implements onclicklistener { @override public void onclick(view v) { //在当前加0.5分 ratingbar.setrating(ratingbar.getrating()+0.5f); } } @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; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ratingbar android:id="@+id/ratingbar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numstars="5" android:stepsize="0.5" /> <button android:id="@+id/buttn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ratingbar1" android:text="评分" /> </linearlayout>
以上就是小编为大家带来的android之ratingbar控件用法详解的全部内容了,希望对大家有所帮助,多多支持~
上一篇: SQL复杂查询(一)