简体   繁体   English

不同活动中的物品不塌陷

[英]Items in different activities not collapsing

I am trying to create a preferences activity but for some reason, my Switch control is not behaving the way that it is supposed to. 我正在尝试创建一个首选项活动,但是由于某种原因,我的Switch控件没有按照预期的方式运行。 It prevents the desired itms from collapsing + my app crashes whenever I navigate to the SettingsActivity. 当我导航到SettingsActivity时,它可以防止所需的itms崩溃,并且我的应用程序崩溃。 I really don't understand why this is happening when the component has clearly been declared. 我真的不明白为什么在明确声明该组件后会发生这种情况。 Below is my code (the code for pages 2 and 3 have been omitted to reduce clutter within my question): 下面是我的代码(第2页和第3页的代码已省略,以减少问题中的混乱情况):

activity_page1.xml 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 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 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 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;
        }
    }
}

Current result 当前结果 在此处输入图片说明

Expected result 预期结果 在此处输入图片说明

You're not attaching squareSwitch to any UI component. 您没有将squareSwitch附加到任何UI组件。 Update your SettingsActivity.java to the following: 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;
    }
}

Also, you don't need to implement CompoundButton.OnCheckedChangeListener and override onCheckedChanged in Page1Activity you might want to remove those from there. 同样,您不需要实现CompoundButton.OnCheckedChangeListener并重写onCheckedChanged中的Page1Activity您可能希望从中删除那些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM