代码之家  ›  专栏  ›  技术社区  ›  Mr. B.

Android(Java):如何在对话框中使用LayoutInflater.inflate()?

  •  1
  • Mr. B.  · 技术社区  · 6 年前

    LayoutInflater 在一段时间内 Dialog 不知道该把什么设定为 参数,即 null 现在。

    我发现 answers 对于 onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle) ,但该方法不适用于 对话 .

    这个 大概是 (ViewGroup) null 为了我。

    我的对话

    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);
        }
    }
    

    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);
    

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

    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);
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  4
  •   AskNilesh    6 年前

    用这个

    setContentView(R.layout.my_dialog);
    

    而不是这个

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

    示例代码

    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();
            }
        }
    }
    

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