Android (Java):如何将 LayoutInflater.inflate() 用于对话框? [英] Android (Java): how to use LayoutInflater.inflate() for Dialogs?

查看:75
本文介绍了Android (Java):如何将 LayoutInflater.inflate() 用于对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Dialog 中使用了 LayoutInflater 并且不知道将什么设置为 2nd 参数,即 null 现在.

我找到了 答案 onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle),但该方法不适用于 Dialog.

伪造null 类似(ViewGroup) null 对我来说不是一种选择.

MyDialog

public class MyDialog extends Dialog 实现 View.OnClickListener {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LayoutInflater inflater = LayoutInflater.from(getContext());查看视图 = inflater.inflate(R.layout.my_dialog, null);//________________ 如何替换那个空值?_____^设置内容视图(视图);}}

错误Infer报告:

MyDialog.java:42: 错误:ERADICATE_PARAMETER_NOT_NULLABLE`LayoutInflater.inflate(...)` 需要参数 2 中的非空值,但参数 `null` 可以为空.(原点:第 42 行的空常量).41. LayoutInflater inflater = LayoutInflater.from(getContext());42. >查看视图 = inflater.inflate(R.layout.dialog_unlock, null);

有什么想法吗?提前致谢!

解决方案

public class MyDialog extends Dialog 实现 View.OnClickListener {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_dialog);按钮 myBtn = findViewById(R.id.my_btn);EditText myTextField = findViewById(R.id.my_et);View.OnClickListener onClickMyBtn = v ->{字符串值 = myTextField.getText().toString();Log.d("MyDialog", String.format("My value: %s", value));解雇();};myBtn.setOnClickListener(onClickMyBtn);}}

解决方案

使用这个

setContentView(R.layout.my_dialog);

代替这个

LayoutInflater inflater = LayoutInflater.from(getContext());查看视图 = inflater.inflate(R.layout.my_dialog, null);设置内容视图(视图);

示例代码

import android.app.Dialog;导入 android.content.Context;导入 android.os.Bundle;导入 android.view.View;导入 android.widget.Button;导入 android.widget.Toast;公共类 MyDialog 扩展 Dialog 实现 View.OnClickListener {公共 MyDialog(上下文上下文){超级(上下文);}按钮 myBtn;@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_dialog);myBtn = findViewById(R.id.my_btn);myBtn.setOnClickListener(this);}@覆盖公共无效onClick(查看视图){如果(视图== myBtn){Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT).show();}}}

然后像这样创建对话框

MyDialog myDialog = new MyDialog(this);myDialog.show();

I'm using the LayoutInflater within a Dialog and don't know what to set as a 2nd parameter, which is null for now.

I found answers for onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle), but that method isn't available for a Dialog.

Faking the null by something like (ViewGroup) null is not an option for me.

MyDialog

public class MyDialog extends Dialog implements View.OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view               = inflater.inflate(R.layout.my_dialog, null); 
        // ________________ How to replace that null? ___________________^

        setContentView(view);
    }
}

Error reported by Infer:

MyDialog.java:42: error: ERADICATE_PARAMETER_NOT_NULLABLE
  `LayoutInflater.inflate(...)` needs a non-null value in parameter 2 but argument `null` can be null. (Origin: null constant at line 42).
  41.           LayoutInflater inflater = LayoutInflater.from(getContext());
  42. >         View view               = inflater.inflate(R.layout.dialog_unlock, null);

Any ideas? Thanks in advance!

Solution

public class MyDialog extends Dialog implements View.OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);

        Button myBtn         = findViewById(R.id.my_btn);
        EditText myTextField = findViewById(R.id.my_et);

        View.OnClickListener onClickMyBtn = v -> {
            String value = myTextField.getText().toString();
            Log.d("MyDialog", String.format("My value: %s", value));
            dismiss();
        };
        myBtn.setOnClickListener(onClickMyBtn);
    }
}

解决方案

Use this

setContentView(R.layout.my_dialog);

Instead of this

LayoutInflater inflater = LayoutInflater.from(getContext());
View view  = inflater.inflate(R.layout.my_dialog, null);
setContentView(view);

SAMPLE CODE

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyDialog extends Dialog implements View.OnClickListener {


    public MyDialog(Context context) {
        super(context);
    }

    Button myBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);

        myBtn = findViewById(R.id.my_btn);
        myBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (view == myBtn) {
            Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT).show();
        }
    }
}

Then create your dialog like this

MyDialog myDialog = new MyDialog(this);
myDialog.show();

这篇关于Android (Java):如何将 LayoutInflater.inflate() 用于对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆