Add buttons to PreferenceFragment

后端 未结 4 1986
野性不改
野性不改 2021-02-14 05:48

I want to add a couple buttons to the bottom of my preferences screen for setting defaults and restoring defaults. This answer doesn\'t cover how to do this using PreferenceFrag

4条回答
  •  情歌与酒
    2021-02-14 05:58

    I meet the same question, and I found a way to handle this issue.

    Overwrite the method onCreateView in PreferenceFragment, using the given the LayoutInfalter by parameters to create your own View, and return this view.

    It's my code. I hope this can be helpful

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
    
            View v = inflater.inflate(R.layout.set_alarm, null);
    
            Button save = (Button)v.findViewById(R.id.save);
            Button revert = (Button)v.findViewById(R.id.revert);
            Button delete = (Button)v.findViewById(R.id.delete);
    
            save.setOnClickListener(this);
            revert.setOnClickListener(this);
            delete.setOnClickListener(this);
    
            if(mId == -1){
                delete.setEnabled(false);
            }
    
            return v;
        }
    

提交回复
热议问题