构建自定义AlertDialog,如何实现按钮点击?

时间:2013-08-06 17:39:19

标签: android button listener adapter

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

此按钮单击完全没有响应。如何使这段代码有效?

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

3 个答案:

答案 0 :(得分:2)

您已注册的onCLickListener,是DialogInterface.OnClickListener吗?如果您已使用View类中的onCLickListener,则此处无效。请重新检查。

你需要做

discardButton.setOnClickListener(new DialogInterface.OnClickListener() {

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

答案 1 :(得分: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();
    }
}

我的更新popup.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>

答案 2 :(得分:0)

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