2014-03-13 4 views
0

Android에서 팝업 창을 닫으려면 버튼을 사용하고 싶지 않습니다. 팝업을 닫아야하는 것보다 화면을 누르기 만하면됩니다. 버튼 만 클릭하면 작동합니다. 닫기 버튼을 클릭하면 닫히지 만 내 팝업 창에 아무 버튼도 표시하지 않으려 고합니다. 화면의 아무 곳이나 클릭하고 싶으면 닫아야합니다.클릭 버튼없이 팝업 창 닫기

MainActivity.java :

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.PopupWindow; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

private Button btn1, btn2; 
private QuickAction quick; 
private PopupWindow window; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btn1 = (Button) findViewById(R.id.button1); 
    btn2 = (Button) findViewById(R.id.button2); 
    btn1.setOnClickListener(btn1_Click); 
    btn2.setOnClickListener(btn2_Click); 
    ActionItem itm1 = new ActionItem(1, "Item1", null); 
    ActionItem itm2 = new ActionItem(2, "Item2", null); 
    quick = new QuickAction(this, QuickAction.VERTICAL); 
    quick.addActionItem(itm1); 
    quick.addActionItem(itm2); 
    quick.setOnActionItemClickListener(quick_Clicked); 
} 

private OnClickListener btn1_Click = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     quick.show(v); 
    } 

}; 

private OnClickListener btn2_Click = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.popup, null); 
      window = new PopupWindow(layout, 500, 400, true); 
      window.showAtLocation(layout,17, 0, 0); 
      Button btn = (Button) layout.findViewById(R.id.buttonclose); 
      btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       window.dismiss(); 
      } 

      }); 
    } 


}; 

private QuickAction.OnActionItemClickListener quick_Clicked = new QuickAction.OnActionItemClickListener() { 

    @Override 
    public void onItemClick(QuickAction source, int pos, int actionId) { 
     if (actionId == 1) { 
      Toast.makeText(MainActivity.this, "Item 1 was clicked", Toast.LENGTH_LONG).show(); 
     } else if (actionId == 2) { 
      Toast.makeText(MainActivity.this, "Item 2 was clicked", Toast.LENGTH_LONG).show(); 
     } 
    } 
}; 
} 

여기이 내 팝업입니다. 어떤 변화가 나를 위해 충분합니다. 팝업 클래스를 변경해야합니까?

public class PopupWindows { 
protected Context mContext; 
public PopupWindow mWindow; 
protected View mRootView; 
protected Drawable mBackground = null; 
protected WindowManager mWindowManager; 

/** 
* Constructor. 
* 
* @param context Context 
*/ 
public PopupWindows(Context context) { 
    mContext = context; 
    mWindow  = new PopupWindow(context); 
    mWindow.setBackgroundDrawable(new BitmapDrawable()); 
    mWindow.setTouchInterceptor(new OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { 
       mWindow.dismiss(); 

       return true; 
      } 

      return false; 
     } 
    }); 

    mWindowManager = (WindowManager)  context.getSystemService(Context.WINDOW_SERVICE); 
} 

/** 
* On dismiss 
*/ 
protected void onDismiss() {   
} 

/** 
* On show 
*/ 
protected void onShow() {  
} 


/** 
* On pre show 
*/ 
protected void preShow() { 
    if (mRootView == null) 
     throw new IllegalStateException("setContentView was not called with a view to display."); 

    onShow(); 

// if (mBackground == null) 
//  mWindow.setBackgroundDrawable(new BitmapDrawable()); 
//else 
//  mWindow.setBackgroundDrawable(new BitmapDrawable()); 

    mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); 
    mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 
    //  mWindow.setTouchable(true); 
// mWindow.setFocusable(true); 
// mWindow.setOutsideTouchable(true); 

    mWindow.setContentView(mRootView); 
} 

/** 
* Set background drawable. 
* 
* @param background Background drawable 
*/ 
public void setBackgroundDrawable(Drawable background) { 
    mWindow.setBackgroundDrawable(background); 
} 

/** 
* Set content view. 
* 
* @param root Root view 
*/ 
public void setContentView(View root) { 
    mRootView = root; 

    mWindow.setContentView(root); 
} 

/** 
* Set content view. 
* 
* @param layoutResID Resource id 
*/ 
public void setContentView(int layoutResID) { 
    LayoutInflater inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    setContentView(inflator.inflate(layoutResID, null)); 
} 

/** 
* Set listener on window dismissed. 
* 
* @param listener 
*/ 
public void setOnDismissListener(PopupWindow.OnDismissListener listener) { 
    mWindow.setOnDismissListener(listener); 
} 

/** 
* Dismiss the popup window. 
*/ 
public void dismiss() { 
    mWindow.dismiss(); 
} 

Popup.xml :

것 같습니다 알았어요 닫기 버튼을 삭제합니다. ı의 succes에는 Altina의 koy 내 popupwindow

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/Black" 
android:gravity="center" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="500dp" 
    android:layout_height="1000dp" 
    android:layout_weight="0.09" 
    android:src="@drawable/karisikwaffle" /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="2dp" 
    android:layout_marginBottom="10dp" 
    android:layout_marginTop="10dp" 
    android:background="#33b5e5" 
    android:orientation="vertical" > 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="15dp" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is a popupWindow" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/light_gray" /> 

    <Button 
     android:id="@+id/buttonclose" 
     android:layout_width="108dp" 
     android:layout_height="wrap_content" 
     android:text="Close" /> 
</LinearLayout> 

</LinearLayout> 
+0

: 당신은 여전히 ​​차단 터치 (이유없이), 당신이 뭔가를해야이 원한다면

기간 동안 다음 스레드에서이를 수행 할 수있는 방법을 게시했습니다. http://stackoverflow.com/questions/5919348/android-making-toast-clickable/23818846#23818846 – user3570982

답변

3
private OnClickListener btn2_Click = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.popup, null); 
      window = new PopupWindow(layout, 500, 400, true); 

     // window.setTouchable(true); 
     // window.setOutsideTouchable(true); 
      window.setBackgroundDrawable (new BitmapDrawable()); 
     // window.setFocusable(false); 
      window.setOutsideTouchable(true); 
      window.setTouchInterceptor(new OnTouchListener() { 
       public boolean onTouch(View v, MotionEvent event) { 
        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         window.dismiss(); 
         return true; 
         } 
        return false; 
        } 
       }); 
      window.showAtLocation(layout,17, 0, 0); 

      Button btn = (Button) layout.findViewById(R.id.buttonclose); 
      btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       window.dismiss(); 
      } 

      }); 

    } 


}; 
0
window.setTouchable(true); 
window.setTouchInterceptor(new OnTouchListener() 
{ 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) 
     { 
      window.dismiss(); 
      return true; 
     } 
     return false;  
    } 
}); 

BUNU window.showAtLocation에 대한 닫기 버튼 않고 닫습니다합니다.

1

팝업이 목록보기 인 경우 허용 된 대답은 실제로 클릭 수신기를 차단하는 버그를 도입합니다. 즉, listview/expandablelistview를 클릭하면 대화 상자가 닫힙니다. 실제로 터치 인터셉터를 설정할 필요가 없습니다. 그것은

window.setBackgroundDrawable(new BitmapDrawable()); 
window.setOutsideTouchable(true); 

의 그 : 같은

는 그래서 코드가 보일 것이다! 당신이 자동 닫기 특정 후 팝업을 할 수있는 방법을 찾고 있다면

window.setTouchInterceptor(new OnTouchListener() { 
    public boolean onTouch(View view, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      window.dismiss(); 
      boolean consumedEvent = true; 
      if (view.hasOnClickListeners()) { 
       consumedEvent = view.onTouchEvent(event); 
      } 
     return consumedEvent; 
    } 
    return false;  
    } 
});