Android弹出窗口不会被忽略

时间:2013-03-13 09:32:36

标签: android button android-ui

我正在尝试在我的应用程序中实现一个弹出窗口,但它不会被忽略。当我点击一个加号按钮时,Popup应该会打开,这是有效的。但是,当我在弹出布局中单击“取消”按钮时它应该关闭,但它没有。为什么呢?

弹出布局(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 个答案:

答案 0 :(得分:2)

该死的!替换单行代码,它将像魅力一样工作。

相反:

PopupWindow popUpWindow = new PopupWindow(inflater.inflate(R.layout.tempo_popup, null, false),250, 150, true);

使用:

View view = inflater.inflate(R.layout.tempo_popup, null, false);
PopupWindow popUpWindow = new PopupWindow(view, 250, 150,  true);

答案 1 :(得分:0)

是否打印弹出取消日志?否则这可能会有所帮助:

Android popup window dismissal