Android自定义的RatingBar,星级评论,带提示文本框还能输入多少字。

public class MainActivity extends Activity {

	private RatingBar ratingBar;
	private RelativeLayout clean;
	private RelativeLayout save;
	private RelativeLayout cancel;
	private EditText commentText;
	private TextView textSum;
	private final int MAX_LENGTH = 30;
	private int Rest_Length = MAX_LENGTH;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        ratingBar = (RatingBar)findViewById(R.id.ratingbar);
        ratingBar.setOnRatingBarChangeListener(new RatingBarListener());
        
        save = (RelativeLayout)findViewById(R.id.save);
        save.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {;
        	  Toast.makeText(MainActivity.this, "抱歉,功能未实现!", Toast.LENGTH_SHORT).show();
        	}
	    }); 
        cancel = (RelativeLayout)findViewById(R.id.cancel);
        cancel.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {;
        	  Toast.makeText(MainActivity.this, "抱歉,功能未实现!", Toast.LENGTH_SHORT).show();
        	}
	    }); 
        
        textSum =(TextView)findViewById(R.id.textSum);
        commentText = (EditText)findViewById(R.id.commentText);
        commentText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(Rest_Length>0){
					Rest_Length = MAX_LENGTH - commentText.getText().length();
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {
			    textSum.setText("还可以输入"+Rest_Length+"个字");
			}
			@Override
			public void afterTextChanged(Editable s) {
				textSum.setText("还可以输入"+Rest_Length+"个字");
			}
		});
        
        clean = (RelativeLayout)findViewById(R.id.clean);
        clean.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				commentText.setText("");
				ratingBar.setRating(0.0f);
				Rest_Length = MAX_LENGTH;
			}
		});
    }
    
    private class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
			    System.out.println("获取的评论级别是:" + rating);
		}
    }
}

源码下载地址:http://download.csdn.net/detail/yuanqihesheng/5805957

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值