Android的對話框常用的有兩種:PopupWindow和AlertDialog。
- popupWindow是一個阻塞式的彈出框,這就意味着在我們退出這個彈出框之前,程序會一直等待,,這就意味着在我們退出這個彈出框之前,程序會一直等待,
- *這和AlertDialog不同哦,AlertDialog是非阻塞式彈出框,AlertDialog彈出的時候,后台可是還可以做其他事情的哦。
PopupWindow顧名思義為彈出菜單,不同於AlertDialog對話框,PopupWindow彈出的位置可以很多變化,按照有無偏移分,可以分為無偏移和偏移兩種;按照參照類型不同又可以分為兩種:相對某個控件(Anchor錨)的位置和父容器內部的相對位置。具體如下:
| 函數 | 簡介 |
| showAsDropDown(View anchor) | 相對某個控件的位置(正左下方),無偏移 |
| showAsDropDown(View anchor, int xoff, int yoff) | 相對某個控件的位置,有偏移(正數表示下方右邊,負數表示(上方左邊)) |
| showAtLocation(View parent, int gravity, int x, int y) | 父容器容器相對位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等 |
下面是運行程序截圖:




程序代碼:
布局:main.xml
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" Android:id="@+id/layout" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <TextView Android:id="@+id/tv_showText" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:gravity="center" Android:text="@string/hello" Android:textSize="22px" /> <Button Android:id="@+id/bt_PopupWindow1" Android:text="以自己為Anchor,不偏移" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> <Button Android:id="@+id/bt_PopupWindow2" Android:text="以自己為Anchor,正下方" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> <Button Android:id="@+id/bt_PopupWindow3" Android:text="以屏幕中心為參照,不偏移(正中間)" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> <Button Android:id="@+id/bt_PopupWindow4" Android:text="以屏幕下方為參照,下方中間" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> </LinearLayout>
自定義對話框dialog.xml
dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <TextView Android:id="@+id/tv_tip" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="請輸入內容:" /> <EditText Android:id="@+id/et_text" Android:layout_width="fill_parent" Android:layout_height="wrap_content" ></EditText> <LinearLayout Android:gravity="center_horizontal" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <Button Android:id="@+id/bt_ok" Android:text="確定" Android:layout_width="100px" Android:layout_height="50px" /> <Button Android:id="@+id/bt_cancle" Android:text="取消" Android:layout_width="100px" Android:layout_height="50px" /> </LinearLayout> </LinearLayout>
import Android.app.Activity;
import Android.content.Context;
import Android.content.SharedPreferences.Editor;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Gravity;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.View.OnClickListener;
import Android.view.ViewGroup.LayoutParams;
import Android.widget.Button;
import Android.widget.EditText;
import Android.widget.Gallery;
import Android.widget.PopupWindow;
import Android.widget.TextView;
public class PopupWindowTest extends Activity {//PopupWindow屬於不阻塞的對話框,AlertDialog則是阻塞的。
private Button bt_popupWindow1;
private Button bt_popupWindow2;
private Button bt_popupWindow3;
private Button bt_popupWindow4;
private TextView tv_showText;
private PopupWindow popupWindow;
private int screenWidth;
private int screenHeight;
private int dialgoWidth;
private int dialgoheight;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
/**
* 初始化控件和響應事件
*/
private void initView() {
bt_popupWindow1 = (Button)findViewById(R.id.bt_PopupWindow1);
bt_popupWindow2 = (Button)findViewById(R.id.bt_PopupWindow2);
bt_popupWindow3 = (Button)findViewById(R.id.bt_PopupWindow3);
bt_popupWindow4 = (Button)findViewById(R.id.bt_PopupWindow4);
tv_showText = (TextView)findViewById(R.id.tv_showText);
bt_popupWindow1.setOnClickListener(new ClickEvent());
bt_popupWindow2.setOnClickListener(new ClickEvent());
bt_popupWindow3.setOnClickListener(new ClickEvent());
bt_popupWindow4.setOnClickListener(new ClickEvent());
}
/**
* 按鈕點擊事件處理
* @author Kobi
*
*/
private class ClickEvent implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.bt_PopupWindow1: //以自己為Anchor,不偏移
getPopupWindow();
popupWindow.showAsDropDown(v);
break;
case R.id.bt_PopupWindow2: //以自己為Anchor,偏移(screenWidth-dialgoWidth)/2, 0)--按鈕正下方
getPopupWindow();
popupWindow.showAsDropDown(v, (screenWidth-dialgoWidth)/2, 0);
break;
case R.id.bt_PopupWindow3: //以屏幕中心為參照,不偏移
getPopupWindow();
popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.CENTER, 0, 0);
break;
case R.id.bt_PopupWindow4: //以屏幕左下角為參照,偏移(screenWidth-dialgoWidth)/2, 0) --屏幕下方中央
getPopupWindow();
popupWindow.showAtLocation(findViewById(R.id.layout),
Gravity.BOTTOM, 0, 0);
break;
default:
break;
}
}
}
/**
* 創建PopupWindow
*/
protected void initPopuptWindow() {
// TODO Auto-generated method stub
View popupWindow_view = getLayoutInflater().inflate( //獲取自定義布局文件dialog.xml的視圖
R.layout.dialog, null,false);
popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);//創建PopupWindow實例
Button bt_ok = (Button)popupWindow_view.findViewById(R.id.bt_ok); //dialog.xml視圖里面的控件
Button bt_cancle = (Button)popupWindow_view.findViewById(R.id.bt_cancle);
final EditText et_text = (EditText)popupWindow_view.findViewById(R.id.et_text);
bt_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv_showText.setText(et_text.getText()); //在標簽里顯示內容
popupWindow.dismiss(); //對話框消失
}
});
bt_cancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
//獲取屏幕和對話框各自高寬
screenWidth = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();
screenHeight = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();
dialgoWidth = popupWindow.getWidth();
dialgoheight = popupWindow.getHeight();
}
/*
* 獲取PopupWindow實例
*/
private void getPopupWindow() {
if(null != popupWindow) {
popupWindow.dismiss();
return;
}else {
initPopuptWindow();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e("ActivityState", "onPause");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("ActivityState", "onResume");
}
}
