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

时间:2022-08-30 07:35:12
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);
		}
    }
}

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

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