【问题标题】:Handle click of custom dialog class with custom view from fragment or activity使用来自片段或活动的自定义视图处理自定义对话框类的单击
【发布时间】:2018-11-21 07:59:41
【问题描述】:

在带有自定义视图的自定义对话框类中,我想处理来自 Activity 或片段的按钮点击,我创建了一个接口来处理按钮点击但显示错误。

尝试在空对象引用上调用接口方法“void com.ymcaspi.util.CustomDialog$DialogInterface.doLogin(com.ymcaspi.util.CustomDialog)”

我的对话框类是

public class CustomDialog extends Dialog implements
    android.view.View.OnClickListener {
public Activity activity;
public Button btnYes, btnNo;
CustomDialog customDialog;
public CustomDialog(Activity activity) {
    super(activity);
    this.activity = activity;
}

DialogInterface dialogInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.alert_login);
    btnYes = (Button) findViewById(R.id.btn_yes);
    btnNo = (Button) findViewById(R.id.btn_no);
    btnYes.setOnClickListener(this);
    btnNo.setOnClickListener(this);
}
//in custom adapter class i want to handle click of button from Activity or fragment, I have created a interface for handling button click
//but showing
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_yes:
            customDialog=new CustomDialog(activity);
            dialogInterface.doLogin(customDialog);
            break;
        case R.id.btn_no:
            dismiss();
            break;
        default:
            break;
    }
    dismiss();
}



  public interface DialogInterface{
        void doLogin(CustomDialog dialog);

   }
}

我已经在fragment中实现了这个接口但是不工作?

【问题讨论】:

  • 显然,您的dialogInterface 从未被分配,您打算如何处理它?
  • dialogInterface 为空,你忘记初始化了。
  • @Aaron 我想在我的任务完成后关闭活动对话框。
  • @farhana 在这种情况下,您可以在同一个类中创建一个方法 doLogin 而无需接口。

标签: android interface dialog


【解决方案1】:

您没有在对话框上初始化 dialogInterface,如果您在 Activity 上实现了接口,请将 Activity 设置为对话框接口

public CustomDialog(Activity activity,DialogInterface dialogInterface ) {
    super(activity);
    this.activity = activity;
    this.dialogInterface = dialogInterface ;
}

【讨论】:

  • ... 或将DialogInterface 的实例传递给构造函数或通过setter
  • @AndreyBusik 可以好好解释一下。
  • 我想在响应想要关闭对话框后调用doLogin 中的服务。
  • @farhana :您的 Activity 中有对话框的实例,因此在收到您的回复后,您可以检查对话框是否显示,然后关闭它。
  • @ArashGM 我这样做了,显示不兼容的类型,我也在片段和活动中实现了接口。
【解决方案2】:

如果您打算从对话框中接收有关您的活动的回调,可以尝试以下示例:

class YourActivity extends Activity implements DialogInterface {

    void showDialog() {
        CustomDialog dialog = // init your CustomDialog
        dialog.setOnLoginClickListener(this);
        dialog.show();
    }

    void doLogin() {
        // Button yes has been clicked, do stuff... 
    }
}

并创建一个方法来在您的CustomDialog 类中分配监听器:

public class CustomDialog extends Dialog implements OnClickListener {

    private DialogInterface dialogInterface;

    public void setOnLoginClickListener(DialogInterface dialogInterface) {
        this.dialogInterface = dialogInterface;
    }
}

【讨论】:

  • 我从你的代码中得到了这个概念,我做了一个小改动,你的setOnLoginClickListener给了我对话框参考。所以我通过了裁判。构造函数中的接口。实际上,我正在将 Activity 与 Interface 进行比较,但我很困惑。
猜你喜欢
  • 1970-01-01
  • 2016-11-03
  • 2014-04-08
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-16
相关资源
最近更新 更多