Android: launch a custom Preference from a PreferenceActivity

前端 未结 4 860
逝去的感伤
逝去的感伤 2020-12-08 23:57

I would like to be able launch a 2nd Preference screen from my PreferenceActivity. And in the 2nd Preference screen I\'d like to use a predefined layout from xml. So, I ha

4条回答
  •  失恋的感觉
    2020-12-09 00:24

    One solution would be to extend a DialogPreference which allows the setting of a custom layout for the preference dialog. This way you have a preference listed and when you tap it, you get a dialog with your custom settings UI.

      
    

    And the class

    class MyPreference extends DialogPreference {
    // along with constructors, you will want to override
        @Override
        protected void onBindDialogView(View view) {
            super.onBindDialogView(view);
            // view is your layout expanded and added to the dialog
                // find and hang on to your views here, add click listeners etc
                // basically things you would do in onCreate
            mTextView = (TextView)view.findViewById(R.Id.mytextview);
            }
    
            @Override
            protected void onDialogClosed(boolean positiveResult) {
               super.onDialogClosed(positiveResult);
    
                if (positiveResult) {
                    // deal with persisting your values here
                }
            }
    }
    

    Obviously there are some other details, but this is the basic idea.

提交回复
热议问题