gpt4 book ai didi

java - 为整个应用设置静音/取消静音复选框

转载 作者:行者123 更新时间:2023-12-03 01:43:32 25 4
gpt4 key购买 nike

您好,我编写了一个测验,一切正常,当我在Dialog2中选中checkBoxmp Checkbox时, Activity 被静音。我设置为当用户失败时,他将定向到第二个 Activity 。有一个按钮“重新开始测验”,使用户重新进入主要 Activity 。问题是当我这样做时,应用程序不再静音了。感谢您的光临!

主要 Activity

             public class QuizActivity extends AppCompatActivity {

private ActionBarDrawerToggle mToggle;

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;
Dialog dialog;
Dialog dialog2;
TextView closeButton;
TextView closeButton2;
CheckBox checkBoxmp;


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

final MediaPlayer mp = new MediaPlayer();
final MediaPlayer mp2 = MediaPlayer.create(this,R.raw.wrong);
final SharedPreferences sharedPref =getPreferences(Context.MODE_PRIVATE);


boolean isChecked = sharedPref.getBoolean("isChecked", false);

if (isChecked){

mp.setVolume(0,0);
mp2.setVolume(0,0);

}


else{mp.setVolume(1,1);}

//Dialog 1
createDialog();
Button dialogButton = (Button) findViewById(R.id.dialogbtn);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();

}
});

closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//end Dialog 1

//Dialog 2
createDialog2();
Button dialogButton2 = (Button) findViewById(R.id.dialogbtn2);
dialogButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog2.show();

}
});


closeButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog2.dismiss();
}
});
//end Dialog 2




//cb


checkBoxmp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("isChecked", isChecked);
editor.commit();
}
})
;


TextView shareTextView = (TextView) findViewById(R.id.share);
shareTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello!");
myIntent.putExtra(Intent.EXTRA_TEXT, "My highscore in Quizzi is very high! I bet you can't beat me except you are cleverer than me. Download the app now! https://play.google.com/store/apps/details?id=amapps.impossiblequiz");
startActivity(Intent.createChooser(myIntent, "Share with:"));
}
});

mQuestionLibrary.shuffle();


setSupportActionBar((Toolbar) findViewById(R.id.nav_action));
DrawerLayout 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) findViewById(R.id.nv1)).setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_stats:
startActivity(new Intent(QuizActivity.this, Menu2.class));
break;
case R.id.nav_about:
startActivity(new Intent(QuizActivity.this, Menu3.class));
break;
}

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);

final List<Button> choices = new ArrayList<>();
choices.add(mButtonChoice1);
choices.add(mButtonChoice2);
choices.add(mButtonChoice3);

updateQuestion();



for (final Button choice : choices) {
choice.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
if (choice.getText().equals(mAnswer)) {


try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("sample.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
updateScore();
updateQuestion();
Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();



} else {
mp2.start();
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);
}
}
});
}

}

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++);
} 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);
startActivity(intent);
}
}

private void updateScore() {
mScoreView.setText(String.valueOf(++mScore));

SharedPreferences mypref = getPreferences(MODE_PRIVATE);
int highScore = mypref.getInt("highScore", 0);

if (mScore > highScore) {
SharedPreferences.Editor editor = mypref.edit();
editor.putInt("highScore", mScore);
editor.apply();
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return mToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}

private void createDialog() {
dialog = new Dialog(this);
dialog.setTitle("Tutorial");
dialog.setContentView(R.layout.popup_menu1_1);
closeButton = (TextView) dialog.findViewById(R.id.closeTXT);
}


private void createDialog2() {
dialog2 = new Dialog(this);
dialog2.setTitle("Settings");
dialog2.setContentView(R.layout.popup_menu1_2);
closeButton2 = (TextView) dialog2.findViewById(R.id.closeTXT2);
checkBoxmp = (CheckBox) dialog2.findViewById(R.id.ckeckBox);


}

}

Menu2(第二个 Activity )
             public class Menu2 extends AppCompatActivity {

private int mScore;
private ActionBarDrawerToggle mToggle;


@Override
public void onBackPressed() {
Intent intent =new Intent(Menu2.this,QuizActivity.class);
startActivity(intent);
}

@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 trophy1 = (ImageView) findViewById(R.id.trophy1);
ImageView trophy2 = (ImageView) findViewById(R.id.trophy2);
ImageView trophy3 = (ImageView) findViewById(R.id.trophy3);
ImageView trophy4 = (ImageView) findViewById(R.id.trophy4);
ImageView trophy5 = (ImageView) findViewById(R.id.trophy5);
ImageView trophy6 = (ImageView) findViewById(R.id.trophy6);
ImageView genius60Score = (ImageView)findViewById(R.id.genius60Img);
ImageView trophyImgBefore60 = (ImageView)findViewById(R.id.statsTrophy);




Intent intent = getIntent();
mScore = intent.getIntExtra("score", 0);
txtScore.setText(String.format("Your score: %d", mScore));

SharedPreferences mypref = getPreferences(MODE_PRIVATE);
int highScore = mypref.getInt("highScore", 0);



if (mScore > highScore) {
highScore = mScore;

txtHighScore.setText(String.format("High score: %d", mScore));

SharedPreferences.Editor editor = mypref.edit();
editor.putInt("highScore", mScore);
editor.apply();
} else {
txtHighScore.setText(String.format("High score: %d", highScore));
}

if (highScore >= 10) {
trophy1.setVisibility(View.VISIBLE);
}

if (highScore >= 20) {
trophy2.setVisibility(View.VISIBLE);
}

if (highScore >= 30) {
trophy3.setVisibility(View.VISIBLE);
}

if (highScore >= 40) {
trophy4.setVisibility(View.VISIBLE);
}

if (highScore >=50) {
trophy5.setVisibility(View.VISIBLE);
}

if (highScore >= 60) {
trophy6.setVisibility(View.VISIBLE);
trophyImgBefore60.setVisibility(View.GONE);
genius60Score.setVisibility(View.VISIBLE);
}

final List<ImageView> containers = new ArrayList<>();
containers.add(trophy1);
containers.add(trophy2);
containers.add(trophy3);
containers.add(trophy4);
containers.add(trophy5);
containers.add(trophy6);

final List<Integer> dialogs = new ArrayList<>();
dialogs.add(R.layout.popup_menu2_1);
dialogs.add(R.layout.popup_menu2_2);
dialogs.add(R.layout.popup_menu2_3);
dialogs.add(R.layout.popup_menu2_4);
dialogs.add(R.layout.popup_menu2_5);
dialogs.add(R.layout.popup_menu2_6);

for (int i = 0; i < containers.size(); i++) {
final int j = i;

containers.get(j).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog dialog = new Dialog(Menu2.this);
dialog.setTitle("Trophies");
dialog.setContentView(dialogs.get(j));
dialog.show();
}
});
}

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

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

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

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home2:
startActivity(new Intent(Menu2.this, QuizActivity.class));
break;
case R.id.nav_about2:
startActivity(new Intent(Menu2.this, Menu3.class));
break;
}

return true;
}
});
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return mToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}

public void onClick(View view) {
Intent intent = new Intent(Menu2.this, QuizActivity.class);
intent.putExtra("score", mScore);
startActivity(intent);
}

}

最佳答案

您需要保留复选框的状态并在初始化mp时读取它,这很容易 SharedPreferences帮助您即使通过重新启动也可以通过所有应用程序存储状态。

像这样声明:

SharedPreferences sharedPref =getPreferences(Context.MODE_PRIVATE);

添加数据并提交:
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("isChecked", false);
editor.commit();

CheckBox上:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("isChecked", isChecked);
editor.commit();
}
})

;

在开始时的每个 Activity 中获取数据:
boolean isChecked = sharedPref.getBoolean("isChecked", false);

关于java - 为整个应用设置静音/取消静音复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com