簡體   English   中英

自定義對話框單擊監聽器android?

[英]Custom dialog click listener android?

我創建了一個擴展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)

代碼有什么問題? 我是否正確實現了接口?

您沒有初始化onButtonClick接口,因此當您嘗試訪問它時它為null導致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;
     }
}

該錯誤是因為您尚未在CustomAlertDialog類中初始化onButtonClick 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();
        }
     }
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM