在android中保存首选项

时间:2014-02-21 12:14:28

标签: android application-settings

我正在尝试编写代码,以便在设置中检查提示选项时它应显示提示,但是当我这样做时,它表示没有定义getContext()方法。这种方法会做什么以及我将在何处定义它?

这是我的数学应用逻辑的代码:

package com.gamesup.braingame;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Easy extends Activity implements OnClickListener{

EditText display;
// This Array says , I am an array that holds arrays
String [][] multiArray = {{"4 + 5 = ", "9"},     
        {"20 * 3 - 1 = ","59"},
        {"99 - 9 = ","90"},
        {"50 / 2 + 18 = ","43"},
        {"9 * 8 = ","72"},
        {"4 + 20 - 20 = ","4"},
        {"75 / 5 = ","15"},
        {"99 - 1 * 3 = ","96"},
        {"75 + 25 = ","100"}};  
TextView displayExpression;
TextView displayAnswer;
TextView setAnswer;

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

    display = (EditText)findViewById(R.id.displayText);
    display.setText("?");

    displayExpression = (TextView) findViewById(R.id.expression);
    displayAnswer = (TextView) findViewById(R.id.answer_status);
    setAnswer = (TextView) findViewById(R.id.answer);
    Button generate = (Button) findViewById(R.id.random_gen);

    generate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Random ranGenerate = new Random ();
            int random = ranGenerate.nextInt(multiArray.length) ;
            // Fetch your random question
            String Rquestion = multiArray[random][0];
            displayExpression.setText(Rquestion);
            displayAnswer.setText("");
            setAnswer.setText("?");

        }
    });

}

static boolean isEmpty = true;

public void num_Clicked(View v){
    Button btn = (Button) findViewById(v.getId());
    //getting the button object and using a view to get the id of the buttons

     if (v.getId()== R.id.del_button){
            String s = display.getText().toString();
            s = s.substring(0, s.length() - 1);
            display.setText(s);
            return;
     }


    if(isEmpty){
        display.setText(btn.getText());
        isEmpty = false;
    }
    else{
        display.append(btn.getText().toString()); 
    }



}

//xml attribute to views called android:onclick, that can be used to handle 
//clicks directly in the view's activity without need to implement any interface.

public void hash_Clicked(View v){

    // Get the Answer from your EditText
    String answer =  display.getText().toString();
    setAnswer.setText(answer);

    // Using a for loop iterate on the base index
    for(int i = 0; i < multiArray.length ; i++)
    {      
        // if the answer is in position 1 of Array [i] 
        if(answer.equals(multiArray[i][1]))
        {
            // We have found the answer, Congratulate the User 
            displayAnswer.setTextColor(getResources().getColor(R.color.green));
            displayAnswer.setText("CORRECT");
            break;

         }else{
             // Tell them how bad they are since they can't solve simple equations!
             displayAnswer.setTextColor(getResources().getColor(R.color.red));
             displayAnswer.setText("INCORRECT");

                         //this is where i am getting the error            
             if(Prefs.getHints(getContext())){ 

             }

         }


    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.brain_game, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.settings:
        startActivity(new Intent(this,Prefs.class));
        return true;
        // more items go here if any 
    }
    return false;
}

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

}


}

Prefs课程:

package com.gamesup.braingame;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.content.Context;


public class Prefs extends PreferenceActivity {

//option names and default values 
private static final String OPT_HINTS = "hints";
private static final boolean OPT_HINTS_DEF = true;

@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
}


/**Get  the current value of the hints option*/
public static boolean getHints (Context context){
    return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(OPT_HINTS, OPT_HINTS_DEF);

}

}

2 个答案:

答案 0 :(得分:2)

只需使用this,您已经在Activity,这是一个Context

if(Prefs.getHints(this)){ 

答案 1 :(得分:1)

不需要使用其他上下文,因为如果您在Activity中调用该函数,则只需传递 yourActivityName.this ,这些也是上下文

所以改变

  if(Prefs.getHints(this)){ 

  if(Prefs.getHints(YourActivityName.this)){