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wvcD4KPHA+TmV3VGltZXJQaWNrZXJEaWFsb2codGhpcywgbmV3IFRpbWVQY2lrZXJEaWFsb2cuT25UaW1lU2V0TGlzdGVuZXIoKXuhrdTayrXP1re9t6jW0LvxyKHKsbzkoa19KTs8L3A+CjxwPjxicj4KPC9wPgo8aDI+wfmhorbUu7C/8rfnJiMyNjY4NDu1xLSwv9o8L2gyPgo8cD7G5Mq1sb7WysnPyNTIu8rHtLC/2qOssqKyu8Tcy+PKx0RpYWxvZ6O7xuSxvtbKyc/Kx0FjdGl2aXR5o6zWu8rHvavL/Nf2s8nBy7bUu7C/8rfnJiMyNjY4NDu2+NLRoaM8L3A+CjxwPtXiuPbKxzxzdHJvbmc+1NpBY3Rpdml0edbQxeTWw8/gudh0aGVtZdb3zOI8L3N0cm9uZz6+zW9rwcuhozwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">
七、popupwindow
Popupwindow也是可以創建類似對話框風格的窗口的,使用簡單,兩個步驟:
1、 popupwindow的構造器創建popupwindow對象
2、 顯示的方式有兩種:
1、 作為某控件的下拉項:showAsDropDown(View v)
2、 指定位置展示:showAtLocation()在指定位置顯示出來
(示例中的重點是點擊外部事件的時候popupwindow會不會消失的問題,設置背景很重要)
packagecom.example.hellopopupwindow;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.MotionEvent;
importandroid.view.View;
import android.view.View.OnClickListener;
importandroid.view.View.OnTouchListener;
importandroid.view.ViewGroup.LayoutParams;
importandroid.widget.Button;
importandroid.widget.PopupWindow;
importandroid.widget.Toast;
public classMainActivity extends Activity {
private Context mContext = null;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View view) {
showPopupWindow(view);
}
});
}
private void showPopupWindow(View view) {
// 一個自定義的布局,作為顯示的內容
View contentView =LayoutInflater.from(mContext).inflate(
R.layout.pop_window, null);
// 設置按鈕的點擊事件
Button button = (Button) contentView.findViewById(R.id.button1);
button.setOnClickListener(newOnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"button is pressed",
Toast.LENGTH_SHORT).show();
}
});
final PopupWindow popupWindow = newPopupWindow(contentView,
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(newOnTouchListener() {
@Override
public boolean onTouch(View v,MotionEvent event) {
Log.i("mengdd","onTouch : ");
return false;
// 這裡如果返回true的話,touch事件將被攔截
// 攔截後 PopupWindow的onTouchEvent不被調用,這樣點擊外部區域無法dismiss
}
});
// 如果不設置PopupWindow的背景,無論是點擊外部區域還是Back鍵都無法dismiss彈框
// 我覺得這裡是API的一個bug
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.selectmenu_bg_downward));
// 設置好參數之後再show
popupWindow.showAsDropDown(view);
}
}