2013-08-06 79 views
0
final AlertDialog builder = new AlertDialog.Builder(activity).create(); 
LayoutInflater factory = LayoutInflater.from(activity); 
View view = factory.inflate(R.layout.inventory_item_pop_up, null); 
builder.setView(view); 

ImageButton discardButton = (ImageButton) view.findViewById(R.id.popup_discard); 

discardButton.setClickable(true); 
discardButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     listener.remove(f); 
     builder.dismiss(); 
    } 
}); 

此按鈕點擊根本沒有響應。我怎樣才能使這段代碼工作?順便說一句,我在適配器中使用這段代碼。構建自定義AlertDialog,如何實現按鈕點擊?

+0

嘗試增加'View',然後調用方法就像這些'discardButton.setOnClickListener(new View.onCl ickListener()...' –

+0

試過了,沒有工作。它已經是一個View.OnClickListener。 – user1810737

+0

有一個問題,你說這個代碼是在一個適配器中。你能確切地知道適配器的位置嗎?你可以多顯示一下你的代碼嗎? – Rajeev

回答

0

好了,這是尷尬的...該代碼是正確的,我只是不調用正確的createDialog()方法。

2

the onCLickListener您已註冊,是DialogInterface.OnClickListener?如果您使用View類中的onCLickListener,那麼這不起作用。請重新檢查。

你需要做的

discardButton.setOnClickListener(new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     listener.remove(f); 
     builder.dismiss(); 
    } 
}); 
+0

OnClickListener不帶一個DialogInterface.OnClickListener。 DialogInterface只適用於我想要添加一個按鈕到dialogwindow本身;這是對話視圖中的一個圖像按鈕。 – user1810737

+0

糟糕...還有一件事,你可以嘗試更改此ImageButton discardButton =(ImageButton)view.findViewById(R.id.popup_discard);''與'ImageButton discardButton =(ImageButton)builder.findViewById(R.id.popup_discard );'? – Rajeev

1

我希望這對你有幫助。我實現這種方式的自定義對話框

public void showUpdateDialog() { 
    updateDialog = new Dialog(this, 
      android.R.style.Theme_Translucent_NoTitleBar_Fullscreen) { 
     @Override 
     public void onBackPressed() { 
      this.dismiss(); 
      loadDataAsync.execute(); 

     } 
    }; 

    updateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    updateDialog.getWindow().setFlags(
      WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    updateDialog.setContentView(R.layout.updatepopup); 
    logger.info("Network error popup on"); 
    updateDialog.setCancelable(true); 

    ImageView dialogImage = (ImageView) updateDialog 
      .findViewById(R.id.dialogheaderimageupdateupdate); 

    dialogImage.setBackgroundResource(R.drawable.infoimage); 

    TextView dialogMesage = (TextView) updateDialog 
      .findViewById(R.id.dialogmessgetextupdate); 
    TextView currVersion = (TextView) updateDialog 
      .findViewById(R.id.currentversionupdate); 
    TextView newVersion = (TextView) updateDialog 
      .findViewById(R.id.newversionupdate); 
    TextView dialogHeader = (TextView) updateDialog 
      .findViewById(R.id.dialogheadertextupdate); 

    newVersion.setText("Latest version: " + versionCode); 
    currVersion.setText("Current version : " + currentVersionName); 
    dialogMesage.setText("Would you like to update now?"); 
    dialogHeader.setText("A program update is available! "); 

    Button dialogOk = (Button) updateDialog 
      .findViewById(R.id.dialogokbuttonupdate); 
    dialogOk.setText("Update"); 
    dialogOk.setFocusable(true); 
    dialogOk.requestFocus(); 
    dialogOk.setFocusableInTouchMode(true); 
    dialogOk.requestFocus(); 

    Button dialogCancel = (Button) updateDialog 
      .findViewById(R.id.dialogcancelbuttonupdate); 
    dialogOk.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      updateDialog.dismiss(); 
      downLoad = new DownLoad(); 
      downLoad.execute(apkUrl.trim()); 
     } 
    }); 
    dialogCancel.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      updateDialog.dismiss(); 
      loadDataAsync.execute(); 

     } 
    }); 
    try { 
     updateDialog.show(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

而我updatepopup.xml下方顯示

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

<LinearLayout 
    android:id="@+id/layoutsample" 
    style="@android:style/Theme.Black.NoTitleBar.Fullscreen" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/dialoghdrbg" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/dialogheaderimageupdateupdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/dialogheadertextupdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|right" 
      android:layout_marginLeft="1dp" 
      android:gravity="center_vertical|right" 
      android:textColor="#000000" 
      android:textSize="25dp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/dialogcontentbg" 
     android:gravity="center_horizontal|center_vertical" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/currentversionupdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:textColor="#ffffff" 
      android:textSize="23dp" 
      android:layout_marginTop="10dp"/> 

     <TextView 
      android:id="@+id/newversionupdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:textColor="#ffffff" 
      android:textSize="23dp" /> 

     <TextView 
      android:id="@+id/dialogmessgetextupdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:textColor="#ffffff" 
      android:textSize="23dp" 

      /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center|center_horizontal" 
      android:layout_marginTop="15dp" 
      android:gravity="center|center_horizontal" 
      android:orientation="horizontal" android:layout_marginBottom="10dp"> 

      <Button 
       android:id="@+id/dialogokbuttonupdate" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="15dp" 
       android:background="@drawable/buttonanimation" 
       android:text="@string/OKButton" 
       android:textSize="20dp" 
       android:textStyle="bold" /> 

      <Button 
       android:id="@+id/dialogcancelbuttonupdate" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="15dp" 
       android:background="@drawable/buttonanimation" 
       android:text="@string/CancelButton" 
       android:textSize="20dp" 
       android:textStyle="bold" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

相關問題