2015-06-26 34 views
0

我创建扩展对话框类自定义对话框单击侦听器android?

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) 

什么是错的代码?我是否正确实现了接口?

+0

你忘了初始化onButtonClick。 – NullByte

回答

1

的错误是因为你没有在CustomAlertDialog初始化onButtonClick类。在CustomAlertDialog中添加此方法。

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

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

customAlertDialog.setListener(this); 

,并检查是否onButtonClick为空或不onClick

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

谢谢你,这只是问题。现在,当我在片段中使用它时出现此错误HomeActivity无法转换为CustomAlertDialog $ OnButtonClick ...我将它初始化为customAlertDialog = new CustomAlertDialog(getActivity()) – WISHY

+0

@WISHY更新我的回答看看 – Kartheek

+0

Thanx很多。它解决了一切 – WISHY

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; 
    } 
} 
+0

谢谢你,这只是问题。现在当我在片段中使用它时出现此错误HomeActivity不能转换为CustomAlertDialog $ OnButtonClick – WISHY

+0

我将它初始化为customAlertDialog = new CustomAlertDialog(getActivity()) – WISHY