【问题标题】:Items in different activities not collapsing不同活动中的项目不折叠
【发布时间】:2017-01-25 20:56:06
【问题描述】:

我正在尝试创建一个首选项活动,但由于某种原因,我的 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;
        }
    }
}

当前结果

预期结果

【问题讨论】:

    标签: java android xml android-preferences android-switch


    【解决方案1】:

    您没有将 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;
        }
    }
    

    此外,您不需要在 Page1Activity 中实现 CompoundButton.OnCheckedChangeListener 和覆盖 onCheckedChanged,您可能希望从那里删除它们。

    【讨论】:

    • 你需要施放它,按 alt + enter,它会显示施放它的建议。还更新了答案。
    • 你知道如何解决这个问题吗?几个月来我一直在尝试解决这个问题(由于缺乏教程):link to other question
    猜你喜欢
    • 2013-06-15
    • 2021-04-02
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 2015-05-12
    • 2016-08-02
    • 2011-04-25
    • 2013-06-01
    相关资源
    最近更新 更多