Make Preference look disabled, but still register clicks

邮差的信 提交于 2020-03-18 10:55:12

问题


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.


回答1:


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
        }
    }

}



回答2:


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;
    }

    ...
}


来源:https://stackoverflow.com/questions/3566303/make-preference-look-disabled-but-still-register-clicks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!