亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 中Popwindow彈出菜單的兩種方法實(shí)例

 更新時(shí)間:2017年03月20日 15:13:38   作者:ztp800201  
這篇文章主要介紹了Android 中Popwindow彈出菜單的兩種方法實(shí)例的相關(guān)資料,這里提供了兩種實(shí)現(xiàn)的方法,并附有實(shí)例代碼,需要的朋友可以參考下

Android 中Popwindow彈出菜單的兩種方法實(shí)例

1.popWindow就是對(duì)話框的一種方式!

此文講解的android中對(duì)話框的一種使用方式,它叫popWindow。

2、popWindow的特性

Android的對(duì)話框有兩種:PopupWindow和AlertDialog。它們的不同點(diǎn)在于:

AlertDialog的位置固定,而PopupWindow的位置可以隨意。

AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的。

PopupWindow的位置按照有無(wú)偏移分,可以分為偏移和無(wú)偏移兩種;按照參照物的不同,可以分為相對(duì)于某個(gè)控件(Anchor錨)和相對(duì)于父控件。

實(shí)例代碼:

方法一的Activity

package com.app.test02; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.PopupWindow; 
import android.widget.Toast; 
 
public class PopwindowLeft extends Activity { 
  // 聲明PopupWindow對(duì)象的引用 
  private PopupWindow popupWindow; 
 
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_popupwindow_main); 
    // 點(diǎn)擊按鈕彈出菜單 
    Button pop = (Button) findViewById(R.id.popBtn); 
    pop.setOnClickListener(popClick); 
  } 
 
  // 點(diǎn)擊彈出左側(cè)菜單的顯示方式 
  OnClickListener popClick = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      // TODO Auto-generated method stub 
      getPopupWindow(); 
      // 這里是位置顯示方式,在屏幕的左側(cè) 
      popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); 
    } 
  }; 
 
  /** 
   * 創(chuàng)建PopupWindow 
   */ 
  protected void initPopuptWindow() { 
    // TODO Auto-generated method stub 
    // 獲取自定義布局文件activity_popupwindow_left.xml的視圖 
    View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null, 
        false); 
    // 創(chuàng)建PopupWindow實(shí)例,200,LayoutParams.MATCH_PARENT分別是寬度和高度 
    popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true); 
    // 設(shè)置動(dòng)畫(huà)效果 
    popupWindow.setAnimationStyle(R.style.AnimationFade); 
    // 點(diǎn)擊其他地方消失 
    popupWindow_view.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
        // TODO Auto-generated method stub 
        if (popupWindow != null && popupWindow.isShowing()) { 
          popupWindow.dismiss(); 
          popupWindow = null; 
        } 
        return false; 
      } 
    }); 
  } 
  /*** 
   * 獲取PopupWindow實(shí)例 
   */ 
  private void getPopupWindow() { 
    if (null != popupWindow) { 
      popupWindow.dismiss(); 
      return; 
    } else { 
      initPopuptWindow(); 
    } 
  } 
} 

方法二的Activity

package com.app.test02; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.PopupWindow; 
 
public class PopwindowLeftNew extends Activity{ 
  private PopupWindow popupWindow; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_popupwindow_main); 
     
    findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        // 獲取自定義布局文件activity_popupwindow_left.xml的視圖 
        View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false); 
        // 創(chuàng)建PopupWindow實(shí)例,200,LayoutParams.MATCH_PARENT分別是寬度和高度 
        popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true); 
        // 設(shè)置動(dòng)畫(huà)效果 
        popupWindow.setAnimationStyle(R.style.AnimationFade); 
        // 這里是位置顯示方式,在屏幕的左側(cè) 
        popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); 
        // 點(diǎn)擊其他地方消失 
        popupWindow_view.setOnTouchListener(new OnTouchListener() { 
          @Override 
          public boolean onTouch(View v, MotionEvent event) { 
            // TODO Auto-generated method stub 
            if (popupWindow != null && popupWindow.isShowing()) { 
              popupWindow.dismiss(); 
              popupWindow = null; 
            } 
            return false; 
          } 
        }); 
      } 
    }); 
     
  } 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論