2013-03-13 70 views
1

我想在我的應用程序中實現一個彈出窗口,但它不會解僱。彈出窗口應該打開時,我點擊一個加號按鈕,這是有效的。但是,它應該關閉,當我點擊彈出佈局中的取消按鈕,但它不。爲什麼?Android彈出窗口不會解僱

彈出佈局(tempo_popup.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="number" > 

     <requestFocus /> 
    </EditText> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/confirmtempo" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/Confirm" /> 

     </LinearLayout> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/canceltempo" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/Cancel" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

Java代碼:

public class MetronomeActivity extends Activity{ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_metronome); 
      final Button plus = (Button) findViewById(R.id.tempop); 
      final Button minus = (Button) findViewById(R.id.tempom); 
      final Button confirm_tempo = (Button) findViewById(R.id.confirmtempo); 

      plus.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        final LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View pop = inflater.inflate(R.layout.tempo_popup, null, false); 
        final PopupWindow pw = new PopupWindow(
        inflater.inflate(R.layout.tempo_popup, null, false), 
             250, 150, true); 

        // The code below assumes that the root container has an id called 'main' 
        pw.showAtLocation(plus, Gravity.CENTER, 0, 0); 
        Button cancel_tempo = (Button) pop.findViewById(R.id.canceltempo); 

        cancel_tempo.setOnClickListener(new View.OnClickListener() { 
             @Override 
             public void onClick(View v) { 
               Log.i("Begin1", "POPUP CANCEL"); 
               pw.dismiss(); 
             } 
        }); 
       } 
      }); 
     } 
} 

回答

2

該死!替換單行代碼,它會像魅力一樣工作。

使用:

最終PopupWindow PW =新PopupWindow(流行音樂,250,150,TRUE);

相反:

最終PopupWindow PW =新PopupWindow( inflater.inflate(R.layout.tempo_popup,NULL,假), 250,150,TRUE);

+0

+1的正確答案。 – MKJParekh 2013-03-13 10:22:18

+0

謝謝!它很棒! – 2013-03-13 11:35:11

0

是否彈出取消日誌得到印?否則,這可能幫助:

Android popup window dismissal

+0

不,LogCat中沒有打印任何內容 – 2013-03-13 09:40:23

+0

當我觸摸外部彈出窗口時關閉該鏈接後會彈出關閉,但取消按鈕仍然不起作用,它既不會在LogCat中打印。 – 2013-03-13 09:46:37