Search code examples
javaandroidintsharedpreferenceslocked

I have some problems in 3 activity for pass int to another class and unlocked level


I have main.xml and main.class that has two buttons named : Level 1 and Level 2

each button has a function to open another class

level1.setOnClickListener(new View.OnClickListener() {

                   @Override
                  public void onClick(View v){
                       // TODO Auto-generated method stub
                       Intent i =new Intent(getApplicationContext(),level1.class);
                       startActivity(i);              

    level2.setOnClickListener(new View.OnClickListener() {

               @Override
              public void onClick(View v){
                   // TODO Auto-generated method stub
                   Intent i =new Intent(getApplicationContext(),level2.class);
                   startActivity(i);             
                    }             
              });   

but the condition is level 2 button are locked but i dont know how to make a locked button, The first thing i want to ask is how to make a locked button

and to unlock level 2 button , level1.class event must completed. in level1.class has code like this

public class level1 extends Activity implements OnClickListener {

    int gamelifes=6, index=0, score=0;
    String [] answer ={"the answer"};

    lifes =(TextView)findViewById(R.id.lifestext);
    lifes.setTextColor(Color.RED);
    lifes.setText(String.valueOf(gamelifes));

    buttonanswer = (Button)findViewById(R.id.buttonanswer);
    buttonanswer.setOnClickListener(this);

    good =(ImageView)findViewById(R.id.youwin);
    good.setOnClickListener(this);

    buttonfinish = (Button)findViewById(R.id.finish);
    buttonfinish.setOnClickListener(this);

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub            

    edittext=(EditText)findViewById(R.id.editText); 
    if(v==buttonanswer) {

        String abc =edittext.getText().toString();              
        if(abc.equalsIgnoreCase(answer[index]))
        {
           good.setVisibility(View.VISIBLE);
           buttonfinish.setVisibility(View.VISIBLE);
           score++;
        }
        else
        {
           gamelifes--;                     
        }

       buttonfinish.setOnClickListener(new View.OnClickListener() {

          public void onClick(View v){
             finish();          
          }
       });  
}

when the answer is right there will be show up some image and a button. and score++; if the answer is correct and gamelifes--; if wrong.

my next question is how to make score and gamelifes values send to level2.class whereas the conditions when buttonfinish Click will finish(); and return to Main.class and unlock the level 2 button in main.class

then how to make a sharedpreference setting to save the unlock button?


Solution

  • First, to make a button not clickable you can use:

    level2.setEnabled(false);
    

    Then, when it can be clicked you would just set it to true.

    Secondly, SharedPreferences can be used to store your values, like score, which can then be accessed at any point in the application.

    To set up SharedPreferences:

    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
    preferences.edit().putInt("score", 100).apply();
    

    Then to get the score later:

    preferences.getInt("score");
    

    So, if you wanted to use a saved boolean to know if the second level is unlocked you could do something like this:

    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
    boolean levelTwoUnlocked = preferences.getBoolean("level2", false);
    
    level2.setEnabled(levelTwoUnlocked);
    

    Or if you want to set a buttons visibility:

    if (levelTwoUnlocked) {
        level2.setVisibility(View.VISIBLE);
    } else {
        level2.setVisibility(View.INVISIBLE);
    }