片段内的getLayoutInflater()

时间:2015-03-13 15:43:20

标签: android android-fragments

我想在片段中实现警报对话框。
我创建了一个名为MyDialog的新类,其中包含:

    public class MyDialog {

    public static AlertDialog.Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
        View view = layoutInflater.inflate(R.layout.userdialog, null);
        ((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
        ((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
        return new AlertDialog.Builder(context).setView(view);
    }
}

然后在我想要使用它的时候,我称这个类为:

AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), R.string.cambioemail, R.string.nuevoemail);
            myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            })
                    .setNegativeButton(R.string.cancelaruserdialog, null)
                    .show();

片段的完整代码:

    public class ConfiguracionFragment extends Fragment implements View.OnClickListener {

    public ConfiguracionFragment() {
    }

    TextView tvEmail,tvPswd;

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

        View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
        tvEmail = (TextView) rootView.findViewById(R.id.tvEmailChange);
        tvPswd = (TextView) rootView.findViewById(R.id.tvPswdChange);
        tvEmail.setOnClickListener(this);
        tvPswd.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tvEmailChange:
               AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), R.string.cambioemail, R.string.nuevoemail);
                myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                    }
                })
                        .setNegativeButton(R.string.cancelaruserdialog, null)
                        .show();
                break;

            case R.id.tvPswdChange:
                     Intent intent2 = new Intent();
                        intent2.setClass(getActivity(), UserDialog.class);
                        intent2.putExtra("PSWD", "PSWD");
                        startActivity(intent2);


                break;
        }

    }
}

我尝试获取LayoutInflater的内容是getActivity().getLayoutInflater(),但它不起作用。

也许我弄清楚了或者逻辑不正确,但我不知道为什么我做错了。

这是错误:

enter image description here

修改

    Error:(40, 69) error: method getLayoutInflater in class Fragment cannot be applied to given types;
required: Bundle
found: no arguments
reason: actual and formal argument lists differ in length

1 个答案:

答案 0 :(得分:5)

public class ConfiguracionFragment extends Fragment implements View.OnClickListener {

Bundle savedInstanceState;
Context context;

public ConfiguracionFragment(Context context) {
    this.context = context;
}

TextView tvEmail,tvPswd;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    this.savedInstanceState =savedInstanceState;
    View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
    tvEmail = (TextView) rootView.findViewById(R.id.tvEmailChange);
    tvPswd = (TextView) rootView.findViewById(R.id.tvPswdChange);
    tvEmail.setOnClickListener(this);
    tvPswd.setOnClickListener(this);

    return rootView;
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.tvEmailChange:
           AlertDialog.Builder myDialog = MyDialog.create(context, getLayoutInflater(savedInstanceState), R.string.cambioemail, R.string.nuevoemail);
            myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            })
                    .setNegativeButton(R.string.cancelaruserdialog, null)
                    .show();
            break;

        case R.id.tvPswdChange:
                 Intent intent2 = new Intent();
                    intent2.setClass(getActivity(), UserDialog.class);
                    intent2.putExtra("PSWD", "PSWD");
                    startActivity(intent2);


            break;
    }

}

}