我正在创建一个Android应用程序。我想要发生的是即使应用程序关闭并且用户重新打开它也会永久禁用该按钮。即使用户按下后退按钮/键并重新打开活动,我也想禁用它。 有没有办法实现它? 代码片段真的会有很大的帮助。 非常感谢你。
这是我的活动类代码
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LuzLvl1 extends Activity {
private int currentQuestion;
private String [] answers;
private Button answerButton;
private TextView scoreTxt;
private EditText answerText;
int scoreText=50;
Button btnAlertDialog, btnListDialog = null;
Button luzon1help1, luzon1help2, luzon1help3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.luzlvl1);
init();
scoreTxt = (TextView)findViewById(R.id.score);
scoreTxt.setText("Score : "+ scoreText);
//help buttons
luzon1help1=(Button)findViewById(R.id.btnL1H1);
luzon1help1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (scoreText>=10){
//if the score is equal or greater than 10;
//the game will give a clue and deduct score
scoreText-=10;
scoreTxt.setText("Score : "+ scoreText);
AlertDialog.Builder builder = new AlertDialog.Builder(LuzLvl1.this);
builder.setTitle("Need some Help?");
builder.setMessage("This is Clue Number 1");
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return; }
});
AlertDialog alert = builder.create();
alert.show();
luzon1help1.setEnabled(false);}
else {
//if the score is 0, no deduction will be made and no clue will be given
scoreText=0;
scoreTxt.setText("Score : "+ scoreText);
}
}
}); //end of help 1
//help 2
luzon1help2=(Button)findViewById(R.id.btnL1H2);
luzon1help2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (scoreText>=10){
//if the score is equal or greater than 10;
//the game will give a clue and deduct score
scoreText-=10;
scoreTxt.setText("Score : "+ scoreText);
AlertDialog.Builder builder = new AlertDialog.Builder(LuzLvl1.this);
builder.setTitle("Need some Help?");
builder.setMessage("This is Clue Number 2");
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return; }
});
AlertDialog alert = builder.create();
alert.show();
luzon1help2.setEnabled(false); }
else {
//if the score is 0, no deduction will be made
//and no clue will be given
scoreText=0;
scoreTxt.setText("Score : "+ scoreText);
}
}
}); //end of help 2
//help 3
luzon1help3=(Button)findViewById(R.id.btnL1H3);
luzon1help3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (scoreText>=10){
//if the score is equal or greater than 10;
//the game will give a clue and deduct score
scoreText-=10;
scoreTxt.setText("Score : "+ scoreText);
AlertDialog.Builder builder = new AlertDialog.Builder(LuzLvl1.this);
builder.setTitle("Need some Help?");
builder.setMessage("This is Clue Number 3");
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return; }
});
AlertDialog alert = builder.create();
alert.show();
luzon1help3.setEnabled(false); }
else {
//if the score is 0, no deduction will be made
//and no clue will be given
scoreText=0;
scoreTxt.setText("Score : "+ scoreText);
}
}
}); //end of help 3
} //end of onCreate
//back button is pressed
//when the back button is pressed from the game, the user will be sent to Chapter Menu
//to prevent the user view the previous questions
public void onBackPressed() {
startActivity(new Intent(LuzLvl1.this, MainMenu.class));
finish();
return;
}
public void init()
{
//correct answer
answers=new String[]{"Katrine Ann"};
//accepts user input
answerText=(EditText)findViewById(R.id.AnswerText);
//checks if the answer is correct
answerButton=(Button)findViewById(R.id.AnswerButton);
answerButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkAnswer();
}
});
}
public boolean isCorrect(String answer)
{ return(answer.equalsIgnoreCase(answers[currentQuestion])); }
public void checkAnswer()
{ String answer=answerText.getText().toString();
if(isCorrect(answer))
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("What are you a GENIUS?!");
builder.setMessage("Nice one, Genius! You have P10!");
builder.setIcon(android.R.drawable.btn_star);
builder.setPositiveButton("Next Level",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String userData = scoreTxt.getText().toString();
Intent intent =new Intent(getApplicationContext(),LuzLvl2.class);
intent.putExtra("parameter name", userData); //this will pass the score to next level
startActivity(intent);} });
AlertDialog alert = builder.create();
alert.show(); // Show Alert Dialog
scoreText+=10; scoreTxt.setVisibility(View.GONE);
scoreTxt.setText("Score : "+ scoreText);
//disable all the buttons and textview
answerText.setEnabled(false);
answerButton.setClickable(false);
}
else
{
Toast.makeText(this, "Wrong! Try again", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
您可以使用SharedPreferences,
在oncreate之前声明
SharedPreferences spStateButton;
SharedPreferences.Editor spEditor;
在oncreate方法上初始化变量
spStateButton= getApplicationContext().getSharedPreferences("Button_State", 0);
spEditor = spStateButton.edit();
将以下方法添加到您的活动中
public void setButtonState(boolean enabled) {
spEditor.putBoolean("btn_state", enabled);
spEditor.commit();
}
public boolean getButtonState(){
return spStateButton.getBoolean("btn_state", true);
}
调用该方法以了解您的按钮状态调用
button.setEnabled(getButtonState());
当您需要禁用按钮调用时
setButtonState(false);