首页 > 解决方案 > How to make a custom Preference layout launch a Dialog Fragment and react to Button?

问题描述

I have made the custom layout for the preference layout with one button and the text view .How to open the dialog fragment when user click on the text view field and also interact the button on the field ?

This is What i want , when the toggle button is off .The person cant open the dialog fragment .Once the toggle button is enabled the dialog fragment will get active and user can edit the value and that will get update in summary.

enter image description here

Preference.xml

<com.example.app.Utils.CustomIPPreference
       android:key="@string/pref_manual_IpKey"
       android:title="Set Device Ip manually"
      android:summary="@string/default_ip" />

CustomIPPreference.java

public class CustomIPPreference extends Preference {

    public CustomIPPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setLayoutResource(R.layout.custom_ip_device_select);
    }

    @Override
    protected void onBindView(View view) {
        LinearLayout layout = view.findViewById(R.id.ManualIPBOX_layout);
        SwitchCompat switchCompat = view.findViewById(R.id.manualIpToggle);

        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("pref_manual_IpKey");
              CustomIpDialog customIpDialog = new CustomIpDialog();
                customIpDialog.show(getFragmentManager(), "Frag"); //getFragmentManager not found error 
            }
        });
        switchCompat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("APP", "TOGGLE");
            }
        });
        super.onBindView(view);
    }

The getFragmentManager is giving me error in the code. If i use Dialog preference i cant add a toggle button in right .How can i make Both interact ?

I am not able to find any reference related to this.Please help me out!!

标签: androidandroid-fragmentsandroid-preferencespreferences

解决方案


推荐阅读