您的位置:首页 > 移动开发 > Android开发

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

2013-07-24 10:55 363 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: