gpt4 book ai didi

android - 如何检查用户是否输入了促销代码?

转载 作者:行者123 更新时间:2023-11-30 02:38:49 25 4
gpt4 key购买 nike

我有一个 Android 应用程序,您只需在其中输入促销代码即可获得完整版本。但是如何在其他 Activity 中检查它是否已输入?这是我的代码:

AlertDialog.Builder alert = new AlertDialog.Builder(Browser.this);
alert.setTitle("Enter your promocode.");
final View input = LayoutInflater.from(Browser.this).inflate(R.layout.edittext, null);
alert.setView(input);
final EditText edit = (EditText) input.findViewById(R.id.edit);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (edit.getText().toString().equalsIgnoreCase("TheSecretcode")) {
isPremium = true;
} else {
isPremium = false;
}
}
});
alert.show();
break;

这是第二个 Activity 的代码。我认为它只是没有更新 SharedPrefs。

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_view_layout);
Uri uri = getIntent() != null ? getIntent().getData() : null;
final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
final boolean isPremium = premiumSettings.getBoolean("isPremium", false);
if(isPremium) {
ImageView img = (ImageView) findViewById(R.id.imageView);
img.setImageURI(uri);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(ImageViewer.this);
builder.setTitle("Error");
builder.setMessage("You are not premium user. Please enter the promocode or buy the full version");
builder.setIcon(R.drawable.holo_dark_action_info);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}

最佳答案

使用SharedPreferences

AlertDialog.Builder alert = new AlertDialog.Builder(Browser.this);
alert.setTitle("Enter your promocode.");
final View input = LayoutInflater.from(Browser.this).inflate(R.layout.edittext, null);
alert.setView(input);
final EditText edit = (EditText) input.findViewById(R.id.edit);
final SharedPreferences premiumSettings= getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
SharedPreferencesEditor.Editor editor= premiumSettings.edit();
if (edit.getText().toString() == "TheSecretCode") {
isPremium = true;
} else {
isPremium = false;
}
editor.putBoolean("isPremium", isPremium);
editor.commit();
}
});
alert.show();
break;

然后在另一个 Activity 中,检索您的首选项:

final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE); // The name is the same as previously
final boolean isPremium = premiumSettings.getBoolean("isPremium", false); // false is default value

关于android - 如何检查用户是否输入了促销代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com