0

I like to save and disable a button, after I use the button (the code line below):

btn1.setEnabled(false);

I already know that I must use SharedPreferences, but I still need help with the code. I have already tried a lot but without success. Thats my Code.

public class Pass extends AppCompatActivity implements View.OnClickListener {

private Button btn1;
private EditText text1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pass);

    btn1 = (Button) findViewById(R.id.button);
    btn1.setOnClickListener(this);

    text1 = (EditText) findViewById(R.id.editText);
    text1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);



}


public void onClick (View view){


    if (text1.getText().toString().equals("Pass)){
        AlertDialog ad = new AlertDialog.Builder(this).create();
        ad.setMessage("Super");
        ad.show();
        Intent intent = new Intent(this,Popup.class);
        startActivity(intent);

        btn1.setEnabled(false);

    }else{
        String message = "Leider falsch";
        Toast.makeText(this,message, Toast.LENGTH_LONG).show();
    }
}

}

Thanks for help

Strecki
  • 19
  • 1
  • 6

1 Answers1

0
public class Pass extends AppCompatActivity implements View.OnClickListener 
{

private Button btn1;
private EditText text1;
private SharedPreferences sharefPref;
private SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass);

btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(this);

text1 = (EditText) findViewById(R.id.editText);
text1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
editor = sharedPref.edit();
}


public void onClick (View view){

if(sharedPref.getBoolean("YourKey"),true)
{
editor.putBoolean("YourKey",false);
editor.commit();

if (text1.getText().toString().equals("Pass)){
    AlertDialog ad = new AlertDialog.Builder(this).create();
    ad.setMessage("Super");
    ad.show();
    Intent intent = new Intent(this,Popup.class);
    startActivity(intent);

}else{
    String message = "Leider falsch";
    Toast.makeText(this,message, Toast.LENGTH_LONG).show();
}

}
}

}
Curio
  • 1,331
  • 2
  • 14
  • 34
  • @Strecki maybe I understood bad, what are you looking for? – Curio Aug 11 '17 at 17:41
  • I think your Code isn´t bad, but when I restart the App the Button "btn1" is still clickable. The Button btn1 must be disabled after the first use also when I restart the App. Thats really tricky. – Strecki Aug 11 '17 at 18:26
  • Okay, now when I click the button, nothing happend. I think the button is permanent disabled now. – Strecki Aug 11 '17 at 20:02
  • @Strecki because you have the sharedpreferences of the previous app. You have to uninstall and then re-install it – Curio Aug 11 '17 at 20:04
  • @Strecki if the answer is useful, accept it then :) – Curio Aug 11 '17 at 20:38