自定义对话框单击监听器android? [英] Custom dialog click listener android?

查看:89
本文介绍了自定义对话框单击监听器android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义对话框,扩展了Dialog类

I created a custom dialog extending the Dialog class

public class CustomAlertDialog extends Dialog implements View.OnClickListener {

private TextView tvAlertTitle, tvAlertMessage;
private Button butAlertOk, butAlertCancel;
private CustomAlertDialog.OnButtonClick onButtonClick;

public CustomAlertDialog(Context context) {
    super(context);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setContentView(R.layout.layout_alert_dialog);
    initViews();
}

//initialise views
private void initViews() {
    tvAlertTitle = (TextView) findViewById(R.id.textViewAlertTitle);
    tvAlertMessage = (TextView) findViewById(R.id.textViewAlertMessage);
    butAlertOk = (Button) findViewById(R.id.buttonAlertOk);
    butAlertCancel = (Button) findViewById(R.id.buttonAlertCancel);
    butAlertOk.setOnClickListener(this);
    butAlertCancel.setOnClickListener(this);
}

//set title for dialog
public void setAlertTitle(String title) {
    tvAlertTitle.setText(title);
}

//set message for dialog
public void setAlertMessage(String message) {
    tvAlertMessage.setText(message);
}



@Override
public void onClick(View v) {
    if (v == butAlertOk) {
        onButtonClick.onOkButtonClick();
    }
    if (v == butAlertCancel) {
        onButtonClick.onCancelButtonClick();
    }
}

public interface OnButtonClick {
    void onOkButtonClick();

    void onCancelButtonClick();
}

}

这是我的XML

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textViewAlertTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="3dp"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:text="asdasdasdas"
    android:textColor="@android:color/holo_red_light" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/holo_red_light" />

<TextView
    android:id="@+id/textViewAlertMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left|center"
    android:paddingBottom="3dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="10dp"
    android:text="asdasdasdaslasdjasldkasldlaskjdlaskjdlasskdjalskdjlsakdjlknxcnvksdfahflksaflkasjlakdlkasjd"
    android:textColor="@android:color/black" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/buttonAlertCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray" />

    <Button
        android:id="@+id/buttonAlertOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="OK" />

</LinearLayout>

它在活动中的实现

public class MainActivity extends AppCompatActivity implements CustomAlertDialog.OnButtonClick {

CustomAlertDialog customAlertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_campaign_request);
    customAlertDialog=new CustomAlertDialog(MainActivity.this);
    customAlertDialog.setAlertTitle("xxxxx");
    customAlertDialog.setAlertMessage("ahdjashd");
    customAlertDialog.show();
}


@Override
public void onOkButtonClick() {
    Logger.e("click", "OK");
}

@Override
public void onCancelButtonClick() {
    Logger.e("click","CANCEL");
    customAlertDialog.dismiss();
}

}

当我单击任何按钮时,都会得到此异常

When I click any of the button I get this exception

java.lang.NullPointerException
        at com.vfirst.ui.CustomAlertDialog.onClick(CustomAlertDialog.java:57)
        at android.view.View.performClick(View.java:4575)
        at android.view.View$PerformClick.run(View.java:18578)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5127)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)

代码有什么问题?我是否正确实现了接口?

What is wrong with the code? Have I implemented the interface correctly?

推荐答案

错误是因为您尚未初始化 onButtonClick CustomAlertDialog 类中的c $ c>。在 CustomAlertDialog 中添加此方法。

The error is because you have not initialized onButtonClick in the CustomAlertDialog class. Add this method in the CustomAlertDialog.

  public void setListener(CustomAlertDialog.OnButtonClic onButtonClick){
   this.onButtonClick = onButtonClick;
  }

从创建此对话框的片段中调用此方法。

Invoke this method from the Fragment where you have created this Dialog.

customAlertDialog.setListener(this);

并检查 onButtonClick 是否为空是否在 onClick

and also check whether is onButtonClick is null or not in onClick

public void onClick(View v) {
    if(onButtonClick !=null){
      if (v == butAlertOk) {
            onButtonClick.onOkButtonClick();
        }
        if (v == butAlertCancel) {
            onButtonClick.onCancelButtonClick();
        }
     }
   }

这篇关于自定义对话框单击监听器android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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