/把文字控件添加監聽,點擊彈出自定義窗口
[java]
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//實例化SelectPicPopupWindow
menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
//顯示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //設置layout在PopupWindow中顯示的位置
}
});
}
其中的mian id 必須在layout 中寫出,不然,找不到id 就不會顯示在屏幕中。
[java]
//為彈出窗口實現監聽類
private OnClickListener itemsOnClick = new OnClickListener(){
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_take_photo:
break;
case R.id.btn_pick_photo:
break;
default:
break;
}
}
};
這個是popwindow的工具類,定義這popwindow的彈出方向的動畫。以及你pop中要顯示的條目。
[java]
mport android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class SelectPicPopupWindow extends PopupWindow {
private Button btn_take_photo, btn_pick_photo, btn_cancel;
private View mMenuView;
public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
//取消按鈕
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//銷毀彈出框
dismiss();
}
});
//設置按鈕監聽
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
//設置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//設置SelectPicPopupWindow彈出窗體的寬
this.setWidth(LayoutParams.FILL_PARENT);
//設置SelectPicPopupWindow彈出窗體的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//設置SelectPicPopupWindow彈出窗體可點擊
this.setFocusable(true);
//設置SelectPicPopupWindow彈出窗體動畫效果
this.setAnimationStyle(R.style.AnimBottom);
//實例化一個ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//設置SelectPicPopupWindow彈出窗體的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener監聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y=(int) event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
return true;
}
});
}
}
[java]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main" //此處的mian 就是mainActivity要聲明的
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="點擊我彈出選擇框"
tools:context=".MainActivity" />
</RelativeLayout>