android中PopupWindow弹出式窗体菜单简单记录(11)

//PopupWindow和PopupMenu 的简单记录
public class MainActivity extends Activity {
	private PopupWindow popupWindow;
	private View contentView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// PopupWindow在Activity一运行就建立,只是要按菜单键才能显示出来
		createPopupWindow();// 初始化建立一个布局菜单方法
	}

	// PopupWindow采用自定义的一个布局,点击主菜单(Menu)才能显示出来
	private void createPopupWindow() {
		// 加载布局,下面会用到这里的一些参数,因此要使用布局生成器,在全局定义
		contentView = getLayoutInflater().inflate(R.layout.popwindows, null);
		// new PopupWindow(contentView, width, height, focusable)//要布局生成器、宽、高、聚焦
		popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);// 能够不写聚焦或者写为false

	}

	// popupWindow显示出来后,三个ImageView能够触发事件
	public void bntClick(View view) {
		switch (view.getId()) {
		case R.id.pop_back:
			// 回到Home
			Intent intent = new Intent();
			intent.setAction(intent.ACTION_MAIN);
			intent.addCategory(intent.CATEGORY_HOME);
			startActivity(intent);
			finish();
			break;
		case R.id.pop_share:
			Intent intent2 = new Intent();
			intent2.setAction(Intent.ACTION_CALL);
			intent2.setData(Uri.parse("tel:10086"));
			startActivity(intent2);
			break;
		case R.id.pop_collent:
			Intent intent3 = new Intent();
			intent3.setAction(Intent.ACTION_SENDTO);
			intent3.setData(Uri.parse("smsto:10086"));
			intent3.putExtra("sms_body", "吃饭了!!");
			startActivity(intent3);
			break;
		}
	}

	// PopupWindow经过menu菜单按钮来触发显示或隐藏
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		// 判断是不是Menu键
		case KeyEvent.KEYCODE_MENU:
			if (popupWindow.isShowing()) {// 按菜单键,若是显示则 隐藏
				popupWindow.dismiss();
			} else {
				popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
			}
			break;
		case KeyEvent.KEYCODE_BACK:
			if (popupWindow.isShowing()) {// 按返回键时,若是显示则 (隐藏)
				popupWindow.dismiss();
				return false;
			}
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
         
	
}

//PopupWindow中的布局文件
<LinearLayout 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" >

    <ImageView
        android:id="@+id/pop_back"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="bntClick"
        android:src="@drawable/contentback" />

    <ImageView
        android:id="@+id/pop_share"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="bntClick"
        android:src="@drawable/contentshare" />

    <ImageView
        android:id="@+id/pop_collent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="bntClick"
        android:src="@drawable/collectcontent" />

</LinearLayout>

运行效果图:点击菜单键才弹出java

相关文章
相关标签/搜索