1

我已经阅读了很多关于这个常见问题的论坛,但无法在我的应用程序中解决它。在我的 android 应用程序中,我需要显示一个弹出窗口,其中有一个具有编辑文本和保存按钮的表单。当用户单击保存按钮时,弹出窗口应刷新。我已经尝试过驳回方法,但是:-(

Java代码:

            LayoutInflater inflater = (LayoutInflater) ListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View popUp_layout = inflater.inflate(R.layout.popup_example,(ViewGroup) findViewById(R.id.popup_form_add_quote));
        final PopupWindow popUp = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false), 430, 300, true);
        popUp.setBackgroundDrawable(new BitmapDrawable());
        popUp.setOutsideTouchable(true);
 //     popUp.showAtLocation(popUp_layout, Gravity.CENTER, 10, 10);
        Button save_Button = (Button) popUp_layout.findViewById(R.id.save_quote_button);
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setView(popUp_layout); 
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        save_Button = (Button) alertDialog.findViewById(R.id.save_quote_button);
        if (save_Button != null) {
            save_Button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    System.out.println("Save button clicked.");
                        popUp.dismiss();

                }
            });
        }

xml/widget_info.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="82dp"
android:minWidth="246dp"
android:updatePeriodMillis="1000" >

可绘制 mdpi/myshape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<stroke android:width="2dp" vandroid:color="#FFFFFFFF" />

<gradient android:angle="225" android:endColor="#DD2ECCFA" android:startColor="#DD000000" />

<corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" />

布局/popup_example.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_form_add_quote"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#C0CAE0"
android:orientation="vertical"
android:padding="10dip" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="Quote" />

<EditText
    android:id="@+id/quote_text"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:gravity="left"
    android:layout_weight="0.80"
    android:ems="10"
    android:inputType="textMultiLine"
    android:lines="4"
    android:maxLines="4" >

    <requestFocus />
</EditText>

<CheckBox
    android:id="@+id/quote_status_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enable/Disable" />

<Button
    android:id="@+id/save_quote_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Save" />

4

1 回答 1

0

使用这个代替 AlertDialog.Builder 类而不是股票 Android 类。

package alt.android.os;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;

@SuppressLint("NewApi") public class CustomAlertDialogBuilder extends AlertDialog.Builder {
    /**
     * Click listeners
     */
    private DialogInterface.OnClickListener mPositiveButtonListener = null;
    private DialogInterface.OnClickListener mNegativeButtonListener = null;
    private DialogInterface.OnClickListener mNeutralButtonListener = null;

    /**
     * Buttons text
     */
    private CharSequence mPositiveButtonText = null;
    private CharSequence mNegativeButtonText = null;
    private CharSequence mNeutralButtonText = null;

    private DialogInterface.OnDismissListener mOnDismissListener = null;

    private Boolean mCancelOnTouchOutside = null;

    public CustomAlertDialogBuilder(Context context) {
        super(context);
    }

    public CustomAlertDialogBuilder setOnDismissListener (DialogInterface.OnDismissListener listener) {
        mOnDismissListener = listener;
        return this;
    }

    @Override
    public CustomAlertDialogBuilder setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) {
        mNegativeButtonListener = listener;
        mNegativeButtonText = text;
        return this;
    }

    @Override
    public CustomAlertDialogBuilder setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) {
        mNeutralButtonListener = listener;
        mNeutralButtonText = text;
        return this;
    }

    @Override
    public CustomAlertDialogBuilder setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) {
        mPositiveButtonListener = listener;
        mPositiveButtonText = text;
        return this;
    }

    @Override
    public CustomAlertDialogBuilder setNegativeButton(int textId, DialogInterface.OnClickListener listener) {
        setNegativeButton(getContext().getString(textId), listener);
        return this;
    }

    @Override
    public CustomAlertDialogBuilder setNeutralButton(int textId, DialogInterface.OnClickListener listener) {
        setNeutralButton(getContext().getString(textId), listener);
        return this;
    }

    @Override
    public CustomAlertDialogBuilder setPositiveButton(int textId, DialogInterface.OnClickListener listener) {
        setPositiveButton(getContext().getString(textId), listener);
        return this;
    }

    public CustomAlertDialogBuilder setCanceledOnTouchOutside (boolean cancelOnTouchOutside) {
        mCancelOnTouchOutside = cancelOnTouchOutside;
        return this;
    }



    @Override
    public AlertDialog create() {
        throw new UnsupportedOperationException("CustomAlertDialogBuilder.create(): use show() instead..");
    }

    @Override
    public AlertDialog show() {
        final AlertDialog alertDialog = super.create();

        DialogInterface.OnClickListener emptyOnClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) { }
        };


        // Enable buttons (needed for Android 1.6) - otherwise later getButton() returns null
        if (mPositiveButtonText != null) {
            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, mPositiveButtonText, emptyOnClickListener);
        }

        if (mNegativeButtonText != null) {
            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, mNegativeButtonText, emptyOnClickListener);
        }

        if (mNeutralButtonText != null) {
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, mNeutralButtonText, emptyOnClickListener);
        }

        // Set OnDismissListener if available
        if (mOnDismissListener != null) {
            alertDialog.setOnDismissListener(mOnDismissListener);
        }

        if (mCancelOnTouchOutside != null) {
            alertDialog.setCanceledOnTouchOutside(mCancelOnTouchOutside);
        }

        alertDialog.show();

        // Set the OnClickListener directly on the Button object, avoiding the auto-dismiss feature
        // IMPORTANT: this must be after alert.show(), otherwise the button doesn't exist..
        // If the listeners are null don't do anything so that they will still dismiss the dialog when clicked
        if (mPositiveButtonListener != null) {
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    mPositiveButtonListener.onClick(alertDialog, AlertDialog.BUTTON_POSITIVE);
                }
            });
        }

        if (mNegativeButtonListener != null) {
            alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    mNegativeButtonListener.onClick(alertDialog, AlertDialog.BUTTON_NEGATIVE);
                }
            });
        }

        if (mNeutralButtonListener != null) {
            alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    mNeutralButtonListener.onClick(alertDialog, AlertDialog.BUTTON_NEUTRAL);
                }
            });
        }

        return alertDialog;
    }   
}
于 2012-09-25T15:52:58.307 回答