package com.example.test1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
////////////////////////////
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
//import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
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 PopupWindow2 extends ActionBarActivity {
////////////////////////////
private Button button;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private View popupWindowView;
private PopupWindow popupWindow;
private LayoutInflater inflater;
////////////////////////////
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window2);
////////////////////////////
button=(Button) findViewById(R.id.button_m);
button.setOnClickListener(new ButtonOnClickListener());
////////////////////////////
}
////////////////////////////
private class ButtonOnClickListener implements OnClickListener{
public void onClick(View v) {
inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
popupWindowView=inflater.inflate(R.layout.activity_popup_window_sub, null);
popupWindow=new PopupWindow(popupWindowView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,true);
//必须要有这句否则弹出popupWindow后监听不到Back键
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAtLocation(findViewById(R.id.button_m), Gravity.NO_GRAVITY, 0, 0);
//让popupWindow获得焦点
popupWindow.setFocusable(true);
//设置动画
//popupWindow.setAnimationStyle(R.style.popupWindowAnimation);
popupWindow.update();
//popupWindow中按钮的处理
button1=(Button) popupWindowView.findViewById(R.id.button1);
button2=(Button) popupWindowView.findViewById(R.id.button2);
button3=(Button) popupWindowView.findViewById(R.id.button3);
button4=(Button) popupWindowView.findViewById(R.id.button4);
button1.setOnClickListener(new ButtonsOnClickListener());
button2.setOnClickListener(new ButtonsOnClickListener());
button3.setOnClickListener(new ButtonsOnClickListener());
button4.setOnClickListener(new ButtonsOnClickListener());
}
}
private class ButtonsOnClickListener implements OnClickListener {
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//System.out.println("点击了按钮1");
Toast.makeText(getApplicationContext(), "提示:点击了按钮1,点击窗口外部关闭窗口!",
Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
System.out.println("点击了按钮2");
break;
case R.id.button3:
System.out.println("点击了按钮3");
break;
case R.id.button4:
//System.out.println("点击了按钮4");
popupWindow.dismiss(); // 关闭窗口
break;
default:
break;
}
}
}
//监听Back事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK) {
if (popupWindow!=null&&popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
//MainActivity.this.finish();
PopupWindow2.this.finish();
}
}
return super.onKeyDown(keyCode, event);
}
////////////////////////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.popup_window2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//////////////////////////////////////////////////////////////
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test1.PopupWindow2" >
<Button
android:id="@+id/button_m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="test" />
</RelativeLayout>
//////////////////////////////////////////////////////////////
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test1.PopupWindow_sub" >
<LinearLayout
android:id="@+id/linerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:text="Button1"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
/>
<Button
android:id="@+id/button2"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:text="Button2"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
/>
<Button
android:id="@+id/button3"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:text="Button3"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
/>
<Button
android:id="@+id/button4"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:text="Button4"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
/>
</LinearLayout>
</RelativeLayout>
//////////////////////////////////////////////////////////////