【问题标题】:How to get an Android Switch status from another class?如何从另一个类获取 Android Switch 状态?
【发布时间】:2015-09-07 15:04:32
【问题描述】:

我正在使用一个 RecyclerView,它有一个 Switch,它允许用户在开关打开时从 A 到 Z 组织列表,在开关关闭时从 Z 到 A 组织列表,以实现我需要的 OnClick 方法到现在每个元素的位置,但元素可能在两个不同的位置(取决于用户选择的排序)所以为了知道元素在哪里,我需要从类中知道 Switch 的状态实现 OnClick 方法。

这是我的 Switch-sorting 实现:

@Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    switchStatus.setText("Sorting alphabetically");


                    Collections.sort(categories, new Comparator<Categories>() {
                        @Override
                        public int compare(Categories lhs, Categories rhs) {
                            return lhs.title.compareTo(rhs.title);
                        }
                    });


                    ca.notifyDataSetChanged();

                }else{
                    switchStatus.setText("Sorting by popularity");

                    Collections.sort(categories, new Comparator<Categories>() {
                        @Override
                        public int compare(Categories rhs, Categories lhs) {
                            return lhs.title.compareTo(rhs.title);
                        }
                    });

                ca.notifyDataSetChanged();
                }

            }
        });


        //check the current state before we display the screen
        if(categoriesSortingSwitch.isChecked()){
            switchStatus.setText("Sorting alphabetically");


            Collections.sort(categories, new Comparator<Categories>() {
                @Override
                public int compare(Categories lhs, Categories rhs) {
                    return lhs.title.compareTo(rhs.title);
                }
            });


        }
        else {
            switchStatus.setText("Sorting by popularity");

            Collections.sort(categories, new Comparator<Categories>() {
                @Override
                public int compare(Categories rhs, Categories lhs) {
                    return lhs.title.compareTo(rhs.title);
                }
            });

        }

    }

//I CREATED THIS METHOD THINKING IN USING IT IN THE OTHER CLASS TO GET THE STATUS
   public boolean getSwitchstatus(){

        if(categoriesSortingSwitch.isChecked()){

            return true;
        }
        else return false;
    }

这是我需要获取状态的另一个类的方法:

@Override
        public void onClick(View v) {
            final Intent intent;

            int position = getAdapterPosition();
            if (position==0){

                if(/** find a way to see if it is on or off and if it is on do this**/){

                intent =  new Intent(context, nose.class);
                context.startActivity(intent);
                }
                else/** if it is off**/{

                }

            }
        }

在简历中,我需要找到一种方法来获取开关的状态,这样我就可以知道元素的位置。

非常感谢。

【问题讨论】:

  • 使用共享首选项。开关打开时保存/创建数据(共享),关闭时将其删除。如果打开,请检查 SharedPreference(您可以为此使用标签),如果匹配,请随心所欲。

标签: java android get android-widget


【解决方案1】:

感谢 MkiiSoft 的建议,这就是我所做的并且完美运行:

    @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked){
                        switchStatus.setText("Sorting alphabetically");


                        Collections.sort(categories, new Comparator<Categories>() {
                            @Override
                            public int compare(Categories lhs, Categories rhs) {
                                return lhs.title.compareTo(rhs.title);
                            }
                        });


                        ca.notifyDataSetChanged();


//JUST ADDED THIS SHARED PREFERENCE WITH THE STRINGS ("it's","on") WHEN THE SWITCH IS ON

                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("it's","on");
                        editor.apply();

                    }else{
                        switchStatus.setText("Sorting by popularity");

                        Collections.sort(categories, new Comparator<Categories>() {
                            @Override
                            public int compare(Categories rhs, Categories lhs) {
                                return lhs.title.compareTo(rhs.title);
                            }
                        });

                    ca.notifyDataSetChanged();

    //JUST ADDED THIS SHARED PREFERENCE WITH THE STRINGS ("it's","off") WHEN THE SWITCH IS OFF

                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("it's", "off");
                        editor.apply();
                    }

                }
            });

然后在我需要现在开关状态的类中,我简单地比较了字符串,并根据字符串的匹配或不匹配打开了一个不同的 Activity,这样:

    public void onClick(View v) {
                final Intent intent;

                int position = getAdapterPosition();

                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                String onoff = preferences.getString("it's", "");

//IF IT IS ON DO THIS
                if(onoff.equalsIgnoreCase("on"))
                {
                    if (position==0){

                        intent =  new Intent(context, nose.class);
                        context.startActivity(intent);
                    }

    //IF IT IS OFF DO THIS
                } else if (onoff.equalsIgnoreCase("off")){

                    if (position==0){

                        intent =  new Intent(context, nose2.class);
                        context.startActivity(intent);
                    }
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多