0

簡単な数独を作ろうとしていますが、続行ボタンに問題があります。アプリを最初に起動すると、ボタンは次のように無効になります。

continueButton.setEnabled (false);

したがって、ユーザーは NewGame ボタンを使用する必要があります。そのボタンは自動的に [続行] ボタンを次のように有効にしました。

continueButton.setEnabled (true);

問題は、終了ボタンまたは電話の戻るボタンを使用してアプリを閉じると、アプリを再起動すると再び無効になる [続行] ボタンのステータスが失われることです。

これは私のコードです:

public class Sudoku extends Activity implements OnClickListener {
private static final String TAG = "Sudoku";
private static final String PROFILE = "stato";
boolean t = false;
SharedPreferences preferences;
int  a , b = 0, c = 0;
View continueButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Set up click listeners for all the buttons
continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);

t = isFirstRun();
if (t == false){
continueButton.setEnabled(false);
}else{
continueButton.setEnabled(true);
}
}


@Override
protected void onResume() {
super.onResume();
Music.play(this, R.raw.main);
}

@Override
protected void onPause() {
super.onPause();
Music.stop(this);

}

public void savePreferences(){
SharedPreferences.Editor ed = preferences.edit();
ed.putBoolean("button", true);
ed.commit();
}

public boolean isFirstRun(){
preferences = PreferenceManager.getDefaultSharedPreferences(this);   
return preferences.getBoolean("button", t);

}


//onClick method

public void onClick(View v) {
switch (v.getId()) {
case R.id.continue_button:
a = 1;
startGame(Game.DIFFICULTY_CONTINUE);
break;
// ...
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
// More buttons go here (if any) ...
case R.id.new_button://button NewGame when pressed enable continueButton
continueButton.setEnabled(true);
a = 0;

openNewGameDialog();
break;
case R.id.exit_button: //exit button


finish();


break;
}
}



@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{       
finish();

}

return super.onKeyDown(keyCode, event);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, 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;
}

/** Ask the user what difficulty level they want */
private void openNewGameDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.new_game_title)
.setItems(R.array.difficulty,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,
int i) {
startGame(i);
}
})
.show();
}

/** Start a new game with the given difficulty level */
@SuppressLint("ParserError")
private void startGame(int i) {

Log.d(TAG, "clicked on " + i);
Intent intent = new Intent(this, Game.class);
intent.putExtra("blocca", a);
intent.putExtra(Game.KEY_DIFFICULTY, i);
startActivity(intent);
}
}
4

2 に答える 2

1

Android のデータ ストレージ オプションをご覧ください。SharedPreferencesメインアクティビティが作成されるたびにブール値を保存し、その値を確認することをお勧めします。

public class MainActivity extends Activity{

    //Class used to retain data
    private SharedPreferences preferences;
    private boolean isFirstRun;

    //initialize the preferences in onCreate()
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        isFirstRun = false;
    }

    //When your application gains the foreground, check to see if it is first time
    @Override
    public void onResume(){
        //if nothing has been stored, returns true since it is the first run.
        isFirstRun = preferences.getBoolean("first",true);
        if(isFirstRun)
            //disable continue button
        else
            //enable continue button
    }

    //Save preferences anytime your application loses the foreground
    @Override
    public void onPause(){
        SharedPreferences.Editor ed = preferences.edit();
            ed.putBoolean("first",isFirstRun);
            ed.commit();
    }
}
于 2012-07-10T15:31:43.503 に答える
0

共有設定を使用して、onResume または OnPause メソッドで変数のステータスを確認できます。

これらの変数は、アプリケーション データをアンインストールまたは削除しない限り、有効に保たれます。

SharedPreferences で変数を作成するには

SharedPreferences getSharedPreferences prefs = (PROFILE, Context.MODE_PRIVATE);

SharedPreferences.Editor prefs.edit editor = ();
editor.putBoolean ("ContinueButtonValue", false);
editor.commit ();

アプリケーションの一時停止後にその値を回復するには:

SharedPreferences prefs = getSharedPreferences (Settigns, Context.MODE_PRIVATE);          
prefs.getBoolean sConfiguration = prefs.getBoolean ("ContinueButtonValue", false);
于 2012-07-10T15:46:37.327 に答える