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

相關文章
相關標籤/搜索