简体   繁体   English

如何永久禁用Android中的按钮?

[英]How to permanently disable a button in android?

i'm creating an android application. 我正在创建一个android应用程序。 what i want to happen is to disable the button permanently even when the application is closed and the user re-open it. 我要发生的事情是即使应用程序关闭并且用户重新打开它,也要永久禁用该按钮。 i also want to disable it even when the user pressed the back button/key and reopen the activity. 我也想禁用它,即使用户按下后退按钮/键并重新打开活动。 is there any way to achieve it? 有什么办法可以实现? code snippets would really be a big help. 代码段确实会对您有很大帮助。 thank you so much in advance. 提前非常感谢您。

btw here's my activity class code 顺便说一句,这是我的活动课程代码

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();
            }
    }
}

you can use SharedPreferences, 您可以使用SharedPreferences,

declare before oncreate 在oncreate之前声明

SharedPreferences spStateButton;
SharedPreferences.Editor spEditor;

initialize the variable on method oncreate 在方法oncreate上初始化变量

spStateButton= getApplicationContext().getSharedPreferences("Button_State", 0);
spEditor = spStateButton.edit();

add the following methods to your activity 将以下方法添加到您的活动中

public void setButtonState(boolean enabled) {
    spEditor.putBoolean("btn_state", enabled);
    spEditor.commit();
}

public boolean getButtonState(){
    return spStateButton.getBoolean("btn_state", true);
}

call the method to know your button state call 调用方法以了解您的按钮状态调用

button.setEnabled(getButtonState());

when you need to disable the button call 当您需要禁用按钮调用时

setButtonState(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM