从自定义对话框 Class 更改 Main UI

change Main UI from custom Dialog Class

我有 2 个 class、Main 和 DialogOrder

主要

public class Main extends Fragment{
ImageView order;


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

     order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               new DialogOrder(getActivity()).show();
            }
        });

  return view;
  }
  public void init(Bundle savedInstanceState) {   
   order = (ImageView) view.findViewById(R.id.order);
   order.setImageResource(R.drawable.orderd);
   RelativeLayout.LayoutParams orderparams = new RelativeLayout.LayoutParams(Main.screenHeight / 8, Main.screenHeight / 8);
   orderparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
   orderparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    orderparams.setMargins(Main.screenHeight / 80, Main.screenHeight / 80, Main.screenHeight / 80, Main.screenHeight / 30);

    order.setLayoutParams(orderparams);


 }

  public void update_UI(){

    order.setVisibility(View.INVISIBLE);

}

}

对话框顺序

public class DialogOrder extends Dialog {
Button button;
Main main;
Activity context;


public DialogOrder(Activity context) {
    super(context);

    this.context = context;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.order_dialog);
 main = new Main();
 button = (Button)findviewbyid(R.id.bd);

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           main.update_UI();
           dismiss();
        }
    });
}}

我想要的是当用户按下对话框中的按钮时设置订单图像不可见 现在代码给我 java.lang.NullPointerException

可能我尝试更新 UI 错误 所以请有人告诉我从 child class 更新 parent UI class 的正确方法是什么?

您可以通过将主片段传递给对话框构造函数来更新主片段,或者您可以使用 Listener/Callback 在主片段和对话框之间进行通信。

最佳做法是使用 Listener/Callback,因为对话 不应 访问调用者。这也是将 Dialog 与 Main 片段分离。

首先,通过对话框中的接口创建监听器:

public class DialogOrder extends Dialog {
  ...
  Activity context;
  private DialogListener listener;

  public interface DialogListener {
    void onButtonClicked();
  }

  public DialogOrder(Activity context, DialogListener listener) {
    super(context);
    this.context = context;
    this.listener = listener;
  }

  ...

}

然后,当按钮点击时调用监听器:

public class DialogOrder extends Dialog {

  Activity context;
  private DialogListener listener;

  public interface DialogListener {
    void onButtonClicked();
  }

  public DialogOrder(Activity context, DialogListener listener) {
    super(context);
    this.context = context;
    this.listener = listener;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           listener.onButtonClicked();
           dismiss();
        }
    });
  }
}

现在,您可以创建与侦听器的对话。像这样:

DialogOrder.DialogListener listener = new DialogOrder.DialogListener() {
   @Ovveride
   public void onButtonClicked() {
     update_UI();
   }
};

DialogOrder dialogOrder = new DialogOrder(getActivity(), listener);
dialogOder.show();

主片段将侦听对话框中单击的按钮。