2012-09-25 56 views
1

我已經閱讀了許多有關這個常見問題的論壇,但無法在我的應用程序中解決它。 在我的android應用程序中,我需要顯示一個彈出窗口,其中有一個窗體具有編輯文本和保存按鈕。當用戶點擊保存按鈕時,應該刷新彈出窗口。我曾嘗試解僱的方法,但:-(Android PopupWindow解僱問題

Javacode:

  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" /> 

回答

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; 
    } 
} 
+0

嗨,我已經複製了上面的代碼,並在我的項目中創建了一個Java文件。但是它顯示了getContext()方法的編譯錯誤。它無法找到它。 –

+0

適合我工作。編譯對API級別15?你如何創建你的對話框? – Leandros

+0

我正在創建popupwindow。哎呀..我有API 10,因爲我正在起訴SDK 2.3.3 :-( –