[英]Items in different activities not collapsing
我正在嘗試創建一個首選項活動,但是由於某種原因,我的Switch控件沒有按照預期的方式運行。 當我導航到SettingsActivity時,它可以防止所需的itms崩潰,並且我的應用程序崩潰。 我真的不明白為什么在明確聲明該組件后會發生這種情況。 下面是我的代碼(第2頁和第3頁的代碼已省略,以減少問題中的混亂情況):
activity_page1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_page1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.apptacularapps.settingsapp.Page1Activity">
<View
android:id="@+id/blue_square"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:background="@drawable/shape_blue_square" />
<View
android:id="@+id/blue_circle"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_blue_circle"
android:layout_marginBottom="20dp"
android:layout_below="@id/blue_square"/>
<View
android:id="@+id/blue_rectangle"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:background="@drawable/shape_blue_rectangle"
android:layout_below="@id/blue_circle"/>
</RelativeLayout>
Page1Activity.java
public class Page1Activity extends AppCompatActivity {
boolean squareState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
}
@Override
public void onResume(){
super.onResume();
loadPreferences();
displaySettings();
}
public void loadPreferences(){
SharedPreferences pref = getSharedPreferences("settings", AppCompatActivity.MODE_PRIVATE);
squareState = pref.getBoolean("square_state", true);
}
public void displaySettings() {
if (squareState) {
findViewById(R.id.blue_square).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.blue_square).setVisibility(View.GONE);
}
}
}
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.packagename.settingsapp.SettingsActivity">
<android.support.v7.widget.SwitchCompat
android:id="@+id/square_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:layout_margin="20dp"
android:text="Show squares"
style="@android:style/TextAppearance.Large" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/circle_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:layout_margin="20dp"
android:text="Show circles"
style="@android:style/TextAppearance.Large"
android:layout_below="@id/square_switch"/>
<android.support.v7.widget.SwitchCompat
android:id="@+id/rectangle_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:layout_margin="20dp"
android:text="Show rectangles"
style="@android:style/TextAppearance.Large"
android:layout_below="@id/circle_switch"/>
</RelativeLayout>
SettingsActivity.java
public class SettingsActivity extends AppCompatActivity {
boolean squareState;
SwitchCompat squareSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
@Override
public void onResume(){
super.onResume();
loadPreferences();
displaySettings();
}
public void loadPreferences(){
SharedPreferences pref = getSharedPreferences("settings", AppCompatActivity.MODE_PRIVATE);
squareState = pref.getBoolean("square_state", true);
}
public void displaySettings(){
squareSwitch.setChecked(squareState);
}
@Override
public void onPause(){
super.onPause();
savePreferences();
}
private void savePreferences(){
SharedPreferences pref = getSharedPreferences("settings", AppCompatActivity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("square_state", squareState);
editor.apply();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
switch(id){
case R.id.square_switch:
squareState = isChecked;
break;
}
}
}
您沒有將squareSwitch
附加到任何UI組件。 將SettingsActivity.java
更新為以下內容:
public class SettingsActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
boolean squareState;
SwitchCompat squareSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
squareSwitch = (SwitchCompat)findViewById(R.id.square_switch); //add this line
squareSwitch.setOnCheckedChangeListener(this); //Also add this
}
@Override
public void onResume(){
super.onResume();
loadPreferences();
displaySettings();
}
public void loadPreferences(){
SharedPreferences pref = getSharedPreferences("settings", AppCompatActivity.MODE_PRIVATE);
squareState = pref.getBoolean("square_state", true);
}
public void displaySettings(){
squareSwitch.setChecked(squareState);
}
@Override
public void onPause(){
super.onPause();
savePreferences();
}
private void savePreferences(){
SharedPreferences pref = getSharedPreferences("settings", AppCompatActivity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("square_state", squareState);
editor.apply();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
switch(id){
case R.id.square_switch:
squareState = isChecked;
break;
}
}
同樣,您不需要實現CompoundButton.OnCheckedChangeListener
並重寫onCheckedChanged
中的Page1Activity
您可能希望從中刪除那些內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.