Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何重写onCreateDialogView()并在dialogpreference中添加自定义视图_Android - Fatal编程技术网

Android 如何重写onCreateDialogView()并在dialogpreference中添加自定义视图

Android 如何重写onCreateDialogView()并在dialogpreference中添加自定义视图,android,Android,我试图在对话框首选项中添加自定义视图,但我不知道如何覆盖oncreatedialogview。请帮帮我 @Override protected View onCreateDialogView() { // TODO Auto-generated method stub LayoutInflater inflt = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVI

我试图在对话框首选项中添加自定义视图,但我不知道如何覆盖
oncreatedialogview
。请帮帮我

@Override
protected View onCreateDialogView() {
    // TODO Auto-generated method stub
    LayoutInflater inflt = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflt.inflate(R.layout.numberpicker,null);
    if(view!=null){
        System.out.println("view error");
    }else{
        System.out.println("view ");
    }
    /*plus=(Button)view.findViewById(R.id.buttonplus1);
    minus=(Button)view.findViewById(R.id.buttonminus1);
    d   isplay=(TextView)view.findViewById(R.id.textView1);
    */return view;
}

您可以重写onCreateDialog方法并添加

Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.layout);

return dialog;

如果通过调用setDialogLayoutResource()在DialogPreference类中指定资源,则不必重写onCreateDialogView()方法。例如:

public class NumberPickerPreference extends DialogPreference
{
    public NumberPickerPreference(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setDialogLayoutResource(R.layout.numberpicker);
    }
}

问题是您覆盖了错误的类

下面是我们都希望创建新对话框首选项的代码:

公共类MyCustomPreference扩展了DialogPreference{

和大多数时候一样,我们使用自动完成来确保我们没有任何打字错误,并且成为一个友好、快速的程序员

但Android Studio创建了以下导入语句:

导入androidx.preference.DialogPreference;

这很好,但是AndroidX类没有
onCreateDialogView()
方法

幸运的是,修复很容易。只需从导入语句中删除“x”,即可获得:

导入android.preference.DialogPreference;

由于有许多不同的库,这类问题很常见,而且随着情况的不断变化,这类问题将变得越来越普遍