關於PopupWindow的兩個問題

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   os   ar   java   

問題描述
第一個問題:PopupWindow裡面的控制項怎麼監聽。
第二個問題:怎麼實現上下兩個PopupWindow同時出現,同時消失,如多看閱讀(見)
我的項目裡是這樣的:點擊出現一個PopupWindow,但是裡面的四個也需要點擊監聽,我應該怎麼做?



我自己是這樣寫的
package com.freeplay.microdiary.activities;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.util.Log;import android.view.Gravity;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;import android.widget.PopupWindow;import android.widget.TextView;import com.freeplay.microdiary.R;import static com.freeplay.microdiary.R.id.edit_All;public class EditActivity extends Activity {    private TextView pic_TextView;    private TextView location_TextView;    private TextView weather_TextView;    private PopupWindow mPopupWindow1;    private PopupWindow mPopupWindow2;    private PopupWindow mPopupWindow3;    private TextView lastPic_TextView;    private TextView takePhoto_TextView;    private TextView cancel_TextView1;    private TextView cancel_TextView2;    private TextView cancel_TextView3;    private TextView pic_Library_TextView;    private Context mContext = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        System.out.println("啟動EditActivity");        super.onCreate(savedInstanceState);        setContentView(R.layout.edit_activity);        mContext=this;        /*        找到控制項         */        pic_TextView = (TextView) findViewById(R.id.pic_TextView);        location_TextView = (TextView) findViewById(R.id.location_TextView);        weather_TextView = (TextView) findViewById(R.id.weather_TextView);        /*        PopupWindow相關         */        View popupView1 = getLayoutInflater().inflate(R.layout.pic_menu, null);        pic_Library_TextView = (TextView) findViewById(R.id.pic_Library_TextView);        pic_Library_TextView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                System.out.println("點擊了從圖庫裡選擇");            }        });        mPopupWindow1 = new PopupWindow(popupView1, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);        mPopupWindow1.setTouchable(true);        mPopupWindow1.setOutsideTouchable(true);        mPopupWindow1.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));        mPopupWindow1.setAnimationStyle(R.style.anim_menu_bottombar);        /*        圖片按鈕監聽器         */        pic_TextView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                System.out.println("圖片按鈕點擊");                /*                呈現三個選項:選取最後一張,拍照,開啟圖庫選擇                 */                mPopupWindow1.showAtLocation(findViewById(R.id.edit_All), Gravity.BOTTOM, 0, 0);            }        });    }}

但是這樣點開就報錯 解決方案1
private class PopupWindows extends PopupWindow {@SuppressWarnings("deprecation")@TargetApi(Build.VERSION_CODES.CUPCAKE)@SuppressLint("NewApi")public PopupWindows(Context mContext, View parent) {super(mContext);View view = View.inflate(mContext,R.layout.zhgl_userinfo_avatarupdate, null);view.startAnimation(AnimationUtils.loadAnimation(mContext,R.anim.fade_ins));LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_bottom_in_2));setWidth(LayoutParams.FILL_PARENT);setHeight(LayoutParams.FILL_PARENT);setBackgroundDrawable(new BitmapDrawable());setFocusable(true);setOutsideTouchable(true);setContentView(view);showAtLocation(parent, Gravity.BOTTOM, 0, 0);update();Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);bt1.setOnClickListener(new OnClickListener() {// 通過相機獲得圖片public void onClick(View v) {Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head2.png")));// intentFromCapture.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,// 1);startActivityForResult(intentFromCapture,CAMERA_REQUEST_CODE);dismiss();}});bt2.setOnClickListener(new OnClickListener() {// 從本地選取圖片public void onClick(View v) {Intent intentFromGallery = new Intent();intentFromGallery.setType("image/*"); // 設定檔案類型intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);startActivityForResult(intentFromGallery,IMAGE_REQUEST_CODE);dismiss();}});bt3.setOnClickListener(new OnClickListener() {public void onClick(View v) {dismiss();}});}}


然後在activity裡 new PopupWindows(UserInfoActivity.this, aiv);

關於PopupWindow的兩個問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.