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

此按钮单击根本没有响应。我怎样才能使这段代码工作?

顺便说一句,我在适配器中使用此代码。

4

3 回答 3

2

onCLickListener你已经注册了,是DialogInterface.OnClickListener吗?如果您onCLickListener从 View 类中使用过,那么这将在这里不起作用。请重新检查。

你需要做

discardButton.setOnClickListener(new DialogInterface.OnClickListener() {

    @Override
    public void onClick(View v) {
        listener.remove(f);
        builder.dismiss();
    }
});
于 2013-08-06T17:52:24.933 回答
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>

于 2013-08-07T09:21:04.837 回答
0

嗯,这很尴尬......代码是正确的,我只是没有调用正确的 createDialog() 方法。

于 2013-08-06T19:14:40.210 回答