自定义对话框单击监听器android?

时间:2015-06-26 06:45:16

标签: android interface dialog

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

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

}

当我点击任何一个按钮时,我得到了这个异常

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)

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

2 个答案:

答案 0 :(得分:1)

您没有初始化onButtonClick界面,因此当您尝试访问它时,它会为NullPointerException。您可以执行以下操作

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

    if (context instanceof CustomAlertDialog.OnButtonClick) {
       onButtonClick = (CustomAlertDialog.OnButtonClick)context;
     }
}

答案 1 :(得分:1)

错误是因为您尚未在onButtonClick课程中初始化CustomAlertDialog。在CustomAlertDialog

中添加此方法
  public void setListener(CustomAlertDialog.OnButtonClic onButtonClick){
   this.onButtonClick = onButtonClick;
  }

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

customAlertDialog.setListener(this);

并检查onButtonClick

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