我试图添加有星级的评级栏进行评分, 我想让率有4星,但在我的代码中,我得到超过7星 当我试图点击一颗星然后出现双..(例如:我点击星1然后显示2颗星)...
我的例子:
我的代码:
public class test extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
textView =(TextView)findViewById(R.id.textView1);
// Button1
final Button btn1 = (Button) findViewById(R.id.btnAddHotelRate);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ShowDialog();
}
});
}
public void ShowDialog()
{
final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
final RatingBar rating = new RatingBar(this);
rating.setMax(4);
popDialog.setIcon(android.R.drawable.btn_star_big_on);
popDialog.setTitle("Add Rating: ");
popDialog.setView(rating);
// Button OK
popDialog.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
textView.setText(String.valueOf(rating.getProgress()));
dialog.dismiss();
}
})
// Button Cancel
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
popDialog.create();
popDialog.show();
}
}

答案 0 :(得分:1)
在你的xml布局中给出
高度为wrap_content
Widhth也是wrap_content
并设置numStars
你需要像这样使用
final RatingBar rating = new RatingBar(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
rating.setLayoutParams(lp);
rating.setNumStars(4);
通过xml解决同样的问题,试试这个
<RatingBar
enter code hereandroid:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="4" android:stepSize="1.0"
android:rating="2.0" />
然后它会起作用......
了解更多信息
https://www.mkyong.com/android/android-rating-bar-example/
希望它会对你有所帮助:)。
答案 1 :(得分:1)
问题是因为 RatingBar 宽度与父视图相匹配 无论星星数量多少都会产生星星, RatingBar 应该 宽度为 wrap_content ,相对于父级。
尝试在 LinearLayout 中放置 RatingBar ,并将LinearLayout添加到弹出对话框中。
public void ShowDialog()
{
final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
LinearLayout linearLayout = new LinearLayout(this);
final RatingBar rating = new RatingBar(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
rating.setLayoutParams(lp);
rating.setNumStars(4);
rating.setStepSize(1);
//add ratingBar to linearLayout
linearLayout.addView(rating);
popDialog.setIcon(android.R.drawable.btn_star_big_on);
popDialog.setTitle("Add Rating: ");
//add linearLayout to dailog
popDialog.setView(linearLayout);
rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
System.out.println("Rated val:"+v);
}
});
// Button OK
popDialog.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
textView.setText(String.valueOf(rating.getProgress()));
dialog.dismiss();
}
})
// Button Cancel
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
popDialog.create();
popDialog.show();
}
答案 2 :(得分:1)
<强> RateThisApp.java 强>
public class RateThisApp {
private static final String TAG = RateThisApp.class.getSimpleName();
private static final String PREF_NAME = "RateThisApp";
private static final String KEY_INSTALL_DATE = "rta_install_date";
private static final String KEY_LAUNCH_TIMES = "rta_launch_times";
private static final String KEY_OPT_OUT = "rta_opt_out";
private static final String KEY_ASK_LATER_DATE = "rta_ask_later_date";
private static Date mInstallDate = new Date();
private static int mLaunchTimes = 0;
private static boolean mOptOut = false;
private static Date mAskLaterDate = new Date();
private static Config sConfig = new Config();
private static Callback sCallback = null;
// Weak ref to avoid leaking the context
private static WeakReference<AlertDialog> sDialogRef = null;
/**
* If true, print LogCat
*/
public static final boolean DEBUG = false;
/**
* Initialize RateThisApp configuration.
* @param config Configuration object.
*/
public static void init(Config config) {
sConfig = config;
}
/**
* Set callback instance.
* The callback will receive yes/no/later events.
* @param callback
*/
public static void setCallback(Callback callback) {
sCallback = callback;
}
/**
* Call this API when the launcher activity is launched.<br>
* It is better to call this API in onCreate() of the launcher activity.
* @param context Context
*/
public static void onCreate(Context context) {
SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
Editor editor = pref.edit();
// If it is the first launch, save the date in shared preference.
if (pref.getLong(KEY_INSTALL_DATE, 0) == 0L) {
storeInstallDate(context, editor);
}
// Increment launch times
int launchTimes = pref.getInt(KEY_LAUNCH_TIMES, 0);
launchTimes++;
editor.putInt(KEY_LAUNCH_TIMES, launchTimes);
log("Launch times; " + launchTimes);
editor.apply();
mInstallDate = new Date(pref.getLong(KEY_INSTALL_DATE, 0));
mLaunchTimes = pref.getInt(KEY_LAUNCH_TIMES, 0);
mOptOut = pref.getBoolean(KEY_OPT_OUT, false);
mAskLaterDate = new Date(pref.getLong(KEY_ASK_LATER_DATE, 0));
printStatus(context);
}
/**
* This API is deprecated.
* You should call onCreate instead of this API in Activity's onCreate().
* @param context
*/
@Deprecated
public static void onStart(Context context) {
onCreate(context);
}
/**
* Show the rate dialog if the criteria is satisfied.
* @param context Context
* @return true if shown, false otherwise.
*/
public static boolean showRateDialogIfNeeded(final Context context) {
if (shouldShowRateDialog()) {
showRateDialog(context);
return true;
} else {
return false;
}
}
/**
* Show the rate dialog if the criteria is satisfied.
* @param context Context
* @param themeId Theme ID
* @return true if shown, false otherwise.
*/
public static boolean showRateDialogIfNeeded(final Context context, int themeId) {
if (shouldShowRateDialog()) {
showRateDialog(context, themeId);
return true;
} else {
return false;
}
}
/**
* Check whether the rate dialog should be shown or not.
* Developers may call this method directly if they want to show their own view instead of
* dialog provided by this library.
* @return
*/
public static boolean shouldShowRateDialog() {
if (mOptOut) {
return false;
} else {
if (mLaunchTimes >= sConfig.mCriteriaLaunchTimes) {
return true;
}
long threshold = TimeUnit.DAYS.toMillis(sConfig.mCriteriaInstallDays); // msec
if (new Date().getTime() - mInstallDate.getTime() >= threshold &&
new Date().getTime() - mAskLaterDate.getTime() >= threshold) {
return true;
}
return false;
}
}
/**
* Show the rate dialog
* @param context
*/
public static void showRateDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
showRateDialog(context, builder);
}
/**
* Show the rate dialog
* @param context
* @param themeId
*/
public static void showRateDialog(final Context context, int themeId) {
AlertDialog.Builder builder = new AlertDialog.Builder(context, themeId);
showRateDialog(context, builder);
}
/**
* Stop showing the rate dialog
* @param context
*/
public static void stopRateDialog(final Context context){
setOptOut(context, true);
}
/**
* Get count number of the rate dialog launches
* @return
*/
public static int getLaunchCount(final Context context){
SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
return pref.getInt(KEY_LAUNCH_TIMES, 0);
}
private static void showRateDialog(final Context context, AlertDialog.Builder builder) {
if (sDialogRef != null && sDialogRef.get() != null) {
// Dialog is already present
return;
}
LinearLayout linearLayout = new LinearLayout(context);
final RatingBar rating = new RatingBar(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
rating.setLayoutParams(lp);
rating.setNumStars(5);
rating.setStepSize(1);
rating.setRating(5);
//add ratingBar to linearLayout
linearLayout.addView(rating);
int titleId = sConfig.mTitleId != 0 ? sConfig.mTitleId :R.string.app_name;
int messageId = sConfig.mMessageId != 0 ? sConfig.mMessageId : R.string.my_own_message;
int cancelButtonID = sConfig.mCancelButton != 0 ? sConfig.mCancelButton : R.string.my_own_cancel;
int thanksButtonID = sConfig.mNoButtonId != 0 ? sConfig.mNoButtonId : R.string.my_own_thanks;
int rateButtonID = sConfig.mYesButtonId != 0 ? sConfig.mYesButtonId : R.string.my_own_rate;
builder.setTitle(titleId);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage(messageId);
builder.setCancelable(sConfig.mCancelable);
builder.setView(linearLayout);
builder.setPositiveButton(rateButtonID, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (sCallback != null) {
sCallback.onYesClicked();
}
String appPackage = context.getPackageName();
String url = "market://details?id=" + appPackage;
if (!TextUtils.isEmpty(sConfig.mUrl)) {
url = sConfig.mUrl;
}
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName())));
}
setOptOut(context, true);
}
});
builder.setNeutralButton(cancelButtonID, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (sCallback != null) {
sCallback.onCancelClicked();
}
clearSharedPreferences(context);
storeAskLaterDate(context);
}
});
builder.setNegativeButton(thanksButtonID, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (sCallback != null) {
sCallback.onNoClicked();
}
setOptOut(context, true);
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (sCallback != null) {
sCallback.onCancelClicked();
}
clearSharedPreferences(context);
storeAskLaterDate(context);
}
});
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
sDialogRef.clear();
}
});
sDialogRef = new WeakReference<>(builder.show());
}
/**
* Clear data in shared preferences.<br>
* This API is called when the "Later" is pressed or canceled.
* @param context
*/
private static void clearSharedPreferences(Context context) {
SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.remove(KEY_INSTALL_DATE);
editor.remove(KEY_LAUNCH_TIMES);
editor.apply();
}
/**
* Set opt out flag.
* If it is true, the rate dialog will never shown unless app data is cleared.
* This method is called when Yes or No is pressed.
* @param context
* @param optOut
*/
private static void setOptOut(final Context context, boolean optOut) {
SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.putBoolean(KEY_OPT_OUT, optOut);
editor.apply();
mOptOut = optOut;
}
/**
* Store install date.
* Install date is retrieved from package manager if possible.
* @param context
* @param editor
*/
private static void storeInstallDate(final Context context, SharedPreferences.Editor editor) {
Date installDate = new Date();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
PackageManager packMan = context.getPackageManager();
try {
PackageInfo pkgInfo = packMan.getPackageInfo(context.getPackageName(), 0);
installDate = new Date(pkgInfo.firstInstallTime);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
editor.putLong(KEY_INSTALL_DATE, installDate.getTime());
log("First install: " + installDate.toString());
}
/**
* Store the date the user asked for being asked again later.
* @param context
*/
private static void storeAskLaterDate(final Context context) {
SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.putLong(KEY_ASK_LATER_DATE, System.currentTimeMillis());
editor.apply();
}
/**
* Print values in SharedPreferences (used for debug)
* @param context
*/
private static void printStatus(final Context context) {
SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
log("*** RateThisApp Status ***");
log("Install Date: " + new Date(pref.getLong(KEY_INSTALL_DATE, 0)));
log("Launch Times: " + pref.getInt(KEY_LAUNCH_TIMES, 0));
log("Opt out: " + pref.getBoolean(KEY_OPT_OUT, false));
}
/**
* Print log if enabled
* @param message
*/
private static void log(String message) {
if (DEBUG) {
Log.v(TAG, message);
}
}
/**
* RateThisApp configuration.
*/
public static class Config {
private String mUrl = null;
private int mCriteriaInstallDays;
private int mCriteriaLaunchTimes;
private int mTitleId = 0;
private int mMessageId = 0;
private int mYesButtonId = 0;
private int mNoButtonId = 0;
private int mCancelButton = 0;
private boolean mCancelable = true;
/**
* Constructor with default criteria.
*/
public Config() {
this(7, 10);
}
/**
* Constructor.
* @param criteriaInstallDays
* @param criteriaLaunchTimes
*/
public Config(int criteriaInstallDays, int criteriaLaunchTimes) {
this.mCriteriaInstallDays = criteriaInstallDays;
this.mCriteriaLaunchTimes = criteriaLaunchTimes;
}
/**
* Set title string ID.
* @param stringId
*/
public void setTitle(@StringRes int stringId) {
this.mTitleId = stringId;
}
/**
* Set message string ID.
* @param stringId
*/
public void setMessage(@StringRes int stringId) {
this.mMessageId = stringId;
}
/**
* Set rate now string ID.
* @param stringId
*/
public void setYesButtonText(@StringRes int stringId) {
this.mYesButtonId = stringId;
}
/**
* Set no thanks string ID.
* @param stringId
*/
public void setNoButtonText(@StringRes int stringId) {
this.mNoButtonId = stringId;
}
/**
* Set cancel string ID.
* @param stringId
*/
public void setCancelButtonText(@StringRes int stringId) {
this.mCancelButton = stringId;
}
/**
* Set navigation url when user clicks rate button.
* Typically, url will be https://play.google.com/store/apps/details?id=PACKAGE_NAME for Google Play.
* @param url
*/
public void setUrl(String url) {
this.mUrl = url;
}
public void setCancelable(boolean cancelable) {
this.mCancelable = cancelable;
}
}
/**
* Callback of dialog click event
*/
public interface Callback {
/**
* "Rate now" event
*/
void onYesClicked();
/**
* "No, thanks" event
*/
void onNoClicked();
/**
* "Later" event
*/
void onCancelClicked();
}
}
简单地呼叫后
RateThisApp.Config config = new RateThisApp.Config(0, 0);
config.setTitle(R.string.app_name);
config.setMessage(R.string.my_own_message);
config.setYesButtonText(R.string.my_own_rate);
config.setNoButtonText(R.string.my_own_thanks);
config.setCancelButtonText(R.string.my_own_cancel);
RateThisApp.init(config);
// Monitor launch times and interval from installation
RateThisApp.onCreate(mContext);
// If the condition is satisfied, "Rate this app" dialog will be shown
RateThisApp.showRateDialog(mContext);
RateThisApp.setCallback(new RateThisApp.Callback() {
@Override
public void onYesClicked() {
// mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
RateThisApp.stopRateDialog(mContext);
}
@Override
public void onNoClicked() {
Toast.makeText(mContext, "No event", Toast.LENGTH_SHORT).show();
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
RateThisApp.stopRateDialog(mContext);
}
@Override
public void onCancelClicked() {
RateThisApp.stopRateDialog(mContext);
}
});
<强>截图强>
答案 3 :(得分:0)
使用rating.setNumStars(4);
代替rating.setMax(4);