从视图创建对话框

Create a Dialog Box from a View

我在代码流之一的 class 中有一个定义为 "public class ChessBoard extends View" 的视图,我想弹出一个对话框,然后从该对话框中获取结果。 我在这里尝试了答案:How to create a Custom Dialog box in android? 但我没有 Activity 传递给构造函数。

因为您的棋盘是一个视图,所以应该有一个 getContext() 方法(参见此处:https://developer.android.com/reference/android/view/View.html#getContext())。您可以将结果转换为 activity:

Activity activity = (Activity) getContext()
Use this code 
///-----------------------------------------------------

 dialog_=new Dialog(this);
                 dialog_.setContentView(R.layout.dialog_submit);//this is path of xml file
                 dialog_.show();
                 Button submit_btn=dialog_.findViewById(R.id.but_submit); //button on dialog
                 Button cancel_btn = dialog_.findViewById(R.id.but_cancel);
                 final EditText edit_username=dialog_.findViewById(R.id.edit_name);