永久禁用按钮单击上的按钮

时间:2019-04-05 20:26:20

标签: android

我一直试图完全禁用按钮(即使在应用程序关闭时也是如此)。

我一直在尝试使用共享首选项完全禁用按钮(即使在应用程序关闭时也是如此),并且可以正常工作。但是,我不小心删除了该文件,无法使它再次工作。我看过来自同一问题的示例,但对我而言不起作用。

com.example.new.app;

import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

SharedPreferences spStateButton;
SharedPreferences.Editor spEditor;
int clickcount=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText passWord = (EditText) findViewById(R.id.edittext);
    final Button loginButton = (Button) findViewById(R.id.button);
    spStateButton= getApplicationContext().getSharedPreferences("Button_State", 0);
    spEditor = spStateButton.edit();


    loginButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if(passWord.getText().toString().equals("123456")) {
                Intent i = new Intent();
                i.setComponent(new ComponentName(MainActivity.this,Unlocked.class));
                startActivity(i);

            }else{
                passWord.setText("");
                Toast.makeText(getBaseContext(),  "Wrong", Toast.LENGTH_LONG).show();
                clickcount=clickcount+1;

            }if (clickcount==5){
                        setButtonState(false);

        }
    };

        public void setButtonState(boolean enabled) {
            spEditor.putBoolean("btn_state", enabled);
            loginButton.setEnabled(getButtonState());

            spEditor.commit();
        }

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



    });

}}

3 个答案:

答案 0 :(得分:2)

您的状态未提交,因此您正在使用旧值。您在设置按钮状态后提交。您可以重复使用设置下一个按钮状态的值

public void setButtonState(boolean enabled) {
    spEditor.putBoolean("btn_state", enabled);
    loginButton.setEnabled(enabled);

    spEditor.commit();
}

您要求解释:

  1. 您正在共享首选项编辑器中将btn_state设置为新值
  2. 您想从共享首选项(spStateButton.getBoolean("btn_state", true);)中读取该值,但由于尚未提交,因此尚不存在。
  3. 您正在将值/写入值提交给共享首选项,这是异步执行的。

答案 1 :(得分:1)

启动应用后,您忘记了致电loginButton.setEnabled(getButtonState());。另外,您需要将setButtonStategetButtonState从onCreate()方法中移出。

这是正确的MainActivity:

import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    SharedPreferences spStateButton;
    SharedPreferences.Editor spEditor;
    int clickcount = 0;

    Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText passWord = (EditText) findViewById(R.id.edittext);
        loginButton = (Button) findViewById(R.id.button);
        spStateButton = getApplicationContext().getSharedPreferences("Button_State", 0);
        spEditor = spStateButton.edit();

        loginButton.setEnabled(getButtonState()); // You forgot it :)

        loginButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (passWord.getText().toString().equals("123456")) {
                    Intent i = new Intent();
                    i.setComponent(new ComponentName(MainActivity.this, Unlocked.class));
                    startActivity(i);

                } else {
                    passWord.setText("");
                    Toast.makeText(getBaseContext(), "Wrong", Toast.LENGTH_LONG).show();
                    clickcount = clickcount + 1;

                }
                if (clickcount == 5) {
                    setButtonState(false);

                }
            }

        });
    }

    public void setButtonState(boolean enabled){
        spEditor.putBoolean("btn_state", enabled);
        loginButton.setEnabled(getButtonState());

        spEditor.commit();
    }

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

答案 2 :(得分:1)

If(isDisabled()){
button.setEnabled(false);}
isDisabled(){
// get state from shrdPref
}