-1

Im trying to use a button click to disable a button.

    if (currentnumber.equals(maxclicks)) {
        butt.setEnabled(false);
    } else {
        currentnumber = currentnumber + 1;
    }

I noticed that the effect is not permanent. Once you leave the app, the button comes back and is still enabled. Please help me disable a button after 5 clicks permanently. And I am trying to do this on a button click. Thank you

David Kim
  • 21
  • 7

1 Answers1

2

You need to be saving that setting somewhere. Then those settings (likely saved in a text file or xml file) should be read into the program & dealt with appropriately.

Every time you restart your program, regardless of what the user interactions were before, its a "clean slate". Thus, you must reference from something (text/xml) that is not cleared on restart.

void createFile() throws IOException{

    Path p = Paths.get("myPath.txt");

    Files.createFile(p);


}

void readFromFile() throws Exception {

    BufferedReader br = new BufferedReader(new FileReader(""));

    ArrayList<String> myStringArray = new ArrayList<>();

    myStringArray.add(br.readLine());

    br.close();

}

Now, you have the strings from the file in your ArrayList<?> myStringArray, and you may read through it & decide what to do with the associated information.

There is, however, a better way to do this using the sharedpreferences as linked by other community members.

Matthew
  • 817
  • 1
  • 13
  • 39