我一直试图设计一个对话来评价我的游戏。我设计了一个看起来像这样的XML文件 -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F5F5F5">
<LinearLayout
android:id="@+id/LL1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Rate My game"
android:textColor="#000000"
android:textSize="24sp" />
</LinearLayout>
<View
android:id="@+id/singleLine"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="@id/LL1"
android:layout_marginBottom="8dp"
android:background="#2196F3" />
<TextView
android:id="@+id/Description_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/singleLine"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="12dp"
android:text="If you enjoy Playing My Game take a moment to rate
it. Thanks for your Support!"
android:textColor="#000000"
android:textSize="16sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Description_text"
android:layout_margin="8dp">
<Button
android:id="@+id/rate_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="RATE"
android:textAllCaps="true"
android:textColor="#424242"
android:textSize="14sp"
/>
<Button
android:id="@+id/remind_me_later_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="REMIND ME LATER"
android:textAllCaps="true"
android:textColor="#424242"
android:textSize="14sp"
/>
<Button
android:id="@+id/never_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="NEVER"
android:textAllCaps="true"
android:textColor="#424242"
android:textSize="14sp"
/>
</LinearLayout>
但是当它用作对话框时,平面按钮会以某种方式转换为常规按钮,如下所示 -
用于显示它的Java代码 -
public class AppRater extends AppCompatActivity {
private final static String APP_TITLE = "My Game";
private final static String APP_PNAME = "com.alala.blabla";
private final static int DAYS_UNTIL_PROMPT = 0;
private final static int LAUNCHES_UNTIL_PROMPT = 1;
public static void app_launched(Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
if (prefs.getBoolean("dontshowagain", false)) {
return;
}
SharedPreferences.Editor editor = prefs.edit();
// Increment launch counter
long launch_count = prefs.getLong("launch_count", 0) + 1;
editor.putLong("launch_count", launch_count);
// Get date of first launch
Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
if (date_firstLaunch == 0) {
date_firstLaunch = System.currentTimeMillis();
editor.putLong("date_firstlaunch", date_firstLaunch);
}
// Wait at least n days before opening
if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
if (System.currentTimeMillis() >= date_firstLaunch +
(DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
showRateDialog(mContext, editor);
}
}
editor.commit();
}
public static void showRateDialog(final Context mContext,
final SharedPreferences.Editor editor) {
final Dialog dialog = new Dialog(mContext, R.style.FullHeightDialog);
dialog.setContentView(R.layout.customapprater);
Button b1 = (Button) dialog.findViewById(R.id.rate_button);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
mContext.startActivity(new Intent(
Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
dialog.dismiss();
}
});
Button b2 = (Button) dialog.findViewById(R.id.remind_me_later_button);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (editor != null) {
editor.clear().commit();
}
dialog.dismiss();
}
});
Button b3 = (Button) dialog.findViewById(R.id.never_button);
b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
dialog.dismiss();
}
});
dialog.show();
}
}
我做错了什么?
答案 0 :(得分:0)
请将对话框更改为AlertDialog。你可以阅读它们here。
public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
new AlertDialog.Builder(mContext)
.setTitle(R.string.rate_dialog_title)
.setMessage(R.string.rate_dialog_message)
.setPositiveButton(R.string.rate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
}
})
.setNegativeButton(R.string.never, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
}
})
.setNeutralButton(R.string.remind_later, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editor != null) {
editor.clear().commit();
}
}
})
.show();
}
有关AlertDialog.Builder的更多信息。