使首选项看起来已禁用,但仍记录点击

发布于 2024-09-16 13:12:13 字数 206 浏览 6 评论 0原文

因此,在我的应用程序的某些状态下,我想在我的设置菜单中禁用某些 CheckBoxPreferences。但是,如果用户单击它们,我希望显示一个解释性的 toast。如果我只是执行 setEnable(false);在 CheckBoxPreference 上,我确实获得了正确的外观和感觉。但我无法在点击时显示吐司。另一方面,我未能手动使 CheckBoxPreference 看起来像是被禁用的。

So during certain states in my app, I want to disable certain CheckBoxPreferences in my setting-menu. However, if the user clicks them, I want an explanatory toast to be shown. If i just do setEnable(false); on the CheckBoxPreference, I do get the right look and feel. But I cant get a toast to be shown on click. On the other hand, I have failed in manually making a CheckBoxPreference look like it is disabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失退 2024-09-23 13:12:13

您也可以仅禁用首选项的视图,而不是禁用首选项。

public class DisabledAppearanceCheckboxPreference extends CheckBoxPreference {

        protected boolean mEnabledAppearance = false;

        public DisabledAppearanceCheckboxPreference(Context context,
                AttributeSet attrs) {
            super(context, attrs);

        }
    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        boolean viewEnabled = isEnabled()&&mEnabledAppearance;
        enableView(view, viewEnabled);
    }

    protected void enableView( View view, boolean enabled){
        view.setEnabled(enabled);
        if ( view instanceof ViewGroup){
            ViewGroup grp = (ViewGroup)view;
            for ( int index = 0; index < grp.getChildCount(); index++)
                enableView(grp.getChildAt(index), enabled);
        }
    }
    public void setEnabledAppearance( boolean enabled){
        mEnabledAppearance = enabled; 
        notifyChanged();
    }
    @Override
    protected void onClick() {
        if ( mEnabledAppearance)
            super.onClick();
        else{
            // show your toast here
        }
    }

}

Instead of disabling the preference, you can as well disable the views of the preference only.

public class DisabledAppearanceCheckboxPreference extends CheckBoxPreference {

        protected boolean mEnabledAppearance = false;

        public DisabledAppearanceCheckboxPreference(Context context,
                AttributeSet attrs) {
            super(context, attrs);

        }
    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        boolean viewEnabled = isEnabled()&&mEnabledAppearance;
        enableView(view, viewEnabled);
    }

    protected void enableView( View view, boolean enabled){
        view.setEnabled(enabled);
        if ( view instanceof ViewGroup){
            ViewGroup grp = (ViewGroup)view;
            for ( int index = 0; index < grp.getChildCount(); index++)
                enableView(grp.getChildAt(index), enabled);
        }
    }
    public void setEnabledAppearance( boolean enabled){
        mEnabledAppearance = enabled; 
        notifyChanged();
    }
    @Override
    protected void onClick() {
        if ( mEnabledAppearance)
            super.onClick();
        else{
            // show your toast here
        }
    }

}
薄荷港 2024-09-23 13:12:13

即使您的首选项被禁用,您也可以接收 OnTouchEvents:

    public class MyPreferenceFragment extends PreferenceFragment {

    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = super.onCreateView(inflater, container, savedInstanceState);
        final ListView listView = (ListView) view.findViewById(android.R.id.list);

        listView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {

                int position = listView.pointToPosition((int) event.getX(), (int) event.getY());
                ListAdapter adapter = listView.getAdapter();
                Preference preference = (Preference) adapter.getItem(position);

                if (!preference.isEnabled())
                    Toast.makeText(getActivity(), "Sorry, this setting is not available!", Toast.LENGTH_LONG).show();  

                return false;
            }
        });


        return view;
    }

    ...
}

Even if your preference is disabled, you can receive OnTouchEvents:

    public class MyPreferenceFragment extends PreferenceFragment {

    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = super.onCreateView(inflater, container, savedInstanceState);
        final ListView listView = (ListView) view.findViewById(android.R.id.list);

        listView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {

                int position = listView.pointToPosition((int) event.getX(), (int) event.getY());
                ListAdapter adapter = listView.getAdapter();
                Preference preference = (Preference) adapter.getItem(position);

                if (!preference.isEnabled())
                    Toast.makeText(getActivity(), "Sorry, this setting is not available!", Toast.LENGTH_LONG).show();  

                return false;
            }
        });


        return view;
    }

    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文