我有两项活动 - MainActivity和Settings。通过按MainActivity中的菜单按钮并选择"设置"来访问设置。在“设置”中,我有一个复选框,我想保存状态并将其传递给MainActivity,如果选中它,它应该显示"这是真的"如果未选中,则应显示"这是假的#34;。但是现在,当我勾选复选框并按下设置中的后退按钮时,没有任何反应。当我再次回到设置时,我意识到复选框并没有保存我强加给它的状态。谁能告诉我我做错了什么?我是java的新手,我希望得到你的指导。
我的MainActivity.java
package sp.com;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import android.app.TabActivity;
import android.widget.TabHost;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.media.MediaPlayer;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void LoadPreferences(){
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isChecked = sharedPref.getBoolean("checked", false);
if (isChecked == true)
{
Toast.makeText(getApplicationContext(), "this is true.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "this is false",
Toast.LENGTH_LONG).show();
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.setting:
Intent intent = new Intent(MainActivity.this, Settings.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
我的设置.java
package sp.com;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class Settings extends Activity {
CheckBox chkbox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.xml.preferences);
chkbox = (CheckBox) findViewById(R.id.backmusic);
chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("checked", isChecked);
editor.commit();
}
});
}
}
我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sp.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/my_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="sp.com.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="sp.com.WebActivity"
android:label="@string/app_name" />
<activity
android:name="sp.com.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="sp.com.Settings"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
先谢谢你们。 问候, 史蒂夫
答案 0 :(得分:0)
如果首选项值为true,则需要检查CheckBox
,因此在此声明之后
chkbox = (CheckBox) findViewById(R.id.backmusic);
添加以下内容
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
chkbox.setChecked(sharedPref.getBoolean("checked", false));
在您的MainActivity
中,您需要调用LoadPreferences
方法
@Override
protected void onResume() {
super.onResume();
LoadPreferences();
}