2013-08-06 3 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 (новый View.onCl ickListener() ... ' –

+0

Пробовал, не работал. Это уже View.OnClickListener. – user1810737

+0

Один вопрос, вы говорите, что этот код находится в адаптере. Можете ли вы сказать, где именно в адаптере? можете ли вы показать больше своего кода? – Rajeev

ответ

0

Ну, это неудобно ... Код корректен, я просто не был вызов метода правой CreateDialog().

2

onCLickListener Вы зарегистрировались, это DialogInterface.OnClickListener? Если вы использовали onCLickListener из класса View, это не сработает. Повторите проверку.

Вам нужно сделать

discardButton.setOnClickListener(new DialogInterface.OnClickListener() { 

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

OnClickListener не принимает диалоговое окно DialogInterface.OnClickListener. DialogInterface работает только в том случае, если я хочу добавить кнопку к самому диалоговому окну; Это изображение в виде диалога. – user1810737

+0

К сожалению, вы можете попробовать изменить это 'ImageButton discardButton = (ImageButton) view.findViewById (R.id.popup_discard);' с 'ImageButton discardButton = (ImageButton) builder.findViewById (R.id.popup_discard) '? – Rajeev

1

Я надеюсь, что это будет полезно для вас. Я реализовать этот способ для пользовательских Dialog

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> 

Смежные вопросы