根据变量设置可见的ImageView

时间:2017-07-18 16:12:32

标签: java android variables imageview

我编写了一个测验应用程序。如果我的Highscore超过10,可以看到ImageView的可见性。它是非常好的唯一问题,在应用程序重新启动后,ImageView不再可见。如何设置10分,ImageView永久存在?主要代码位于测验活动中,在Menu2中,ImageView应永久可见。

测验活动:

package amapps.impossiblequiz;

            import android.content.Intent;
            import android.os.Bundle;
            import android.support.design.widget.NavigationView;
            import android.support.v4.widget.DrawerLayout;
            import android.support.v7.app.ActionBarDrawerToggle;
            import android.support.v7.app.AppCompatActivity;
            import android.support.v7.widget.Toolbar;
            import android.view.MenuItem;
            import android.view.View;
            import android.widget.Button;
            import android.widget.TextView;
            import android.widget.Toast;

            public class QuizActivity extends AppCompatActivity {


private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private MenuItem menuItem;
private Intent in;

private QuestionLibrary mQuestionLibrary = new QuestionLibrary();

private TextView mScoreView;
private TextView mQuestionView;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;

private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;

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

    //Randromize the row of the questions
    QuestionLibrary q = new QuestionLibrary();
    System.out.printf("Question:0 Choice:(%s, %s, %s) Answer:%s%n",
            q.getChoice1(0), q.getChoice2(0), q.getChoice3(0), q.getCorrectAnswer(0));
    q.shuffle();
    System.out.printf("Question:0 Choice:(%s, %s, %s) Answer:%s%n",
            q.getChoice1(0), q.getChoice2(0), q.getChoice3(0), q.getCorrectAnswer(0));

    //End randomizer

    mToolbar = (Toolbar) findViewById(R.id.nav_action);

    setSupportActionBar(mToolbar);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //Able to see the Navigation Burger "Button"


    NavigationView mNavigationView = (NavigationView) findViewById(R.id.nv1);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem){
            switch (menuItem.getItemId()){
                case(R.id.nav_stats):
                    Intent accountActivity = new Intent(getApplicationContext(),Menu2.class);
                    startActivity(accountActivity);
            }
            return true;
        }
    });



        mScoreView = (TextView) findViewById(R.id.score_score);
        mQuestionView = (TextView) findViewById(R.id.question);
        mButtonChoice1 = (Button) findViewById(R.id.choice1);
        mButtonChoice2 = (Button) findViewById(R.id.choice2);
        mButtonChoice3 = (Button) findViewById(R.id.choice3);


        updateQuestion();

        //Start of Button Listener1
        mButtonChoice1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //My logic for Button goes in here

                if (mButtonChoice1.getText() == mAnswer) {
                    mScore = mScore + 1;
                    updateScore(mScore);
                    updateQuestion();
                    mQuestionLibrary.shuffle();


                    //This line of code is optional...
                    Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show();


                    Intent intent = new Intent(QuizActivity.this, Menu2.class);
                    intent.putExtra("score",mScore); //pass score to Menu2
                    startActivity(intent);




                }
            }


        });
        //End of Button Listener1

        //Start of Button Listener2
        mButtonChoice2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //My logic for Button goes in here

                if (mButtonChoice2.getText() == mAnswer) {
                    mScore = mScore + 1;
                    updateScore(mScore);
                    updateQuestion();
                    mQuestionLibrary.shuffle();



                    //This line of code is optional...
                    Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(QuizActivity.this, "Oh... wrong your score is 0", Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(QuizActivity.this, Menu2.class);
                    intent.putExtra("score",mScore); //pass score to Menu2
                    startActivity(intent);




                }
            }


        });
        //End of Button Listener2

        //Start of Button Listener3
        mButtonChoice3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //My logic for Button goes in here

                if (mButtonChoice3.getText() == mAnswer) {
                    mScore = mScore + 1;
                    updateScore(mScore);
                    updateQuestion();
                    mQuestionLibrary.shuffle();



                    //This line of code is optional...
                    Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(QuizActivity.this, "Come on, that was not so hard...", Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(QuizActivity.this, Menu2.class);
                    intent.putExtra("score",mScore); //pass score to Menu2
                    startActivity(intent);




                }
            }


        });
        //End of Button Listener3

    }


private void updateQuestion() {

    if (mQuestionNumber < mQuestionLibrary.getLength()) {
        mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
        mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber));
        mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber));
        mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber));

        mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
        mQuestionNumber++;
    }
    else {
        Toast.makeText(QuizActivity.this, "Last Question! You are very intelligent!", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(QuizActivity.this, Menu2.class);
        intent.putExtra("score",mScore); //pass score to Menu2
        startActivity(intent);


    }

}

    private void updateScore ( int point){
        mScoreView.setText("" + mScore);

    }


    @Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
    public boolean onOptionsItemSelected (MenuItem item){
        if (mToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

菜单2:

package amapps.impossiblequiz;

        import android.content.Intent;
        import android.content.SharedPreferences;
        import android.os.Bundle;
        import android.support.design.widget.NavigationView;
        import android.support.v4.widget.DrawerLayout;
        import android.support.v7.app.ActionBarDrawerToggle;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.Toolbar;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.ImageView;
        import android.widget.TextView;

        import static amapps.impossiblequiz.R.id.nv2;

        public class Menu2 extends AppCompatActivity {




private DrawerLayout mDrawerLayout2;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;


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


     TextView txtScore = (TextView) findViewById(R.id.textScore2);
     TextView txtHighScore = (TextView)findViewById(R.id.textHighScore);
     ImageView imgView = (ImageView)findViewById(R.id.trophy1);

    Intent intent = getIntent();
    int mScore = intent.getIntExtra ("score",0);
    txtScore.setText("Your score is: " + mScore);
    int applyView = 0;

    SharedPreferences mypref =getPreferences(MODE_PRIVATE);
    int highScore = mypref.getInt("highScore", 0);
    if (highScore>= mScore)
        txtHighScore.setText("High score: " + highScore);


    else{
        txtHighScore.setText("New highscore: " + mScore);
        applyView = mScore;
        SharedPreferences.Editor editor = mypref.edit();
        editor.putInt("highScore",mScore);
        editor.commit();


    }

    if (applyView >10)
        imgView.setVisibility(View.VISIBLE);





    mToolbar = (Toolbar)findViewById(R.id.nav_action);
    setSupportActionBar(mToolbar);
    mDrawerLayout2 = (DrawerLayout) findViewById(R.id.drawerLayout2);

    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout2, R.string.open, R.string.close);
    mDrawerLayout2.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NavigationView mNavigationView = (NavigationView) findViewById(nv2);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem){
            switch (menuItem.getItemId()){
                case(R.id.nav_home2):
                    Intent accountActivity2 = new Intent(getApplicationContext(),QuizActivity.class);
                    startActivity(accountActivity2);

            }
            return true;
        }
    });



}




@Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
public boolean onOptionsItemSelected(MenuItem item) {
    if (mToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);


}
}

3 个答案:

答案 0 :(得分:0)

if (highScore >10)
        imgView.setVisibility(View.VISIBLE);

当您在applyView上进行测试时,如果highScore>= mScore

,则可以保持为0

保存这样的数据:

SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE); 

private void updateScore ( int point){
    mScoreView.setText("" + mScore);
    if(mScore > 10){
       Editor editor = sharedpreferences.edit();
       editor.putInt("score", mScore);
       editor.commit();
   }
}

答案 1 :(得分:0)

在If子句中:

{
 "filename" : "myfilename"
}

您不会更改if (highScore >= mScore) 的值。因此,当第一次保存首选项值且重新启动应用程序时,满足If子句,因此applyView始终等于0.意味着

applyView

此时从未满足。

答案 2 :(得分:0)

尝试更改此内容:

private void updateScore ( int point){
        mScoreView.setText("" + mScore);

}

到此:

private void updateScore ( int point){
        mScoreView.setText("" + mScore);

        SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
        Editor editor = sharedpreferences.edit();
        editor.putInt("currentscore", mScore);
        editor.commit();
}

在你的Menu2中更改:

int applyView = 0;

要:

SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
int applyView =sharedpreferences.getInt("currentscore",0);

编辑:

删除此行:

applyView = mScore;