Android高手進階教程(十)之—-Android PopupWindow的使用!

來源:互聯網
上載者:User

大家好,我們這一節講的是Android PopupWindow的使用! 在我理解其實PopupWindow其實類似於一個不能動的Widget(僅從顯示效果來說!)

它是浮在別的視窗之上的.

下面我將給大家做一個簡單的Demo,類似於音樂播放器的Widget的效果,點擊Button的時候出來PopupWindow,首先我們看一下:

下面是核心代碼:

 

view plaincopy to clipboardprint?
  1. package com.android.tutor;      
  2. import android.app.Activity;      
  3. import android.content.Context;      
  4. import android.os.Bundle;      
  5. import android.view.Gravity;      
  6. import android.view.LayoutInflater;      
  7. import android.view.View;      
  8. import android.view.View.OnClickListener;      
  9. import android.view.ViewGroup.LayoutParams;      
  10. import android.widget.Button;      
  11. import android.widget.PopupWindow;      
  12. public class PopupWindowDemo extends Activity  implements OnClickListener{      
  13.     private Button btn;      
  14.           
  15.     public void onCreate(Bundle savedInstanceState) {      
  16.         super.onCreate(savedInstanceState);      
  17.         setContentView(R.layout.main);      
  18.               
  19.         btn = (Button)findViewById(R.id.btn);      
  20.         btn.setOnClickListener(this);      
  21.     }      
  22.     @Override     
  23.     public void onClick(View v) {      
  24.         Context mContext = PopupWindowDemo.this;      
  25.         if (v.getId() == R.id.btn) {      
  26.             LayoutInflater mLayoutInflater = (LayoutInflater) mContext      
  27.                     .getSystemService(LAYOUT_INFLATER_SERVICE);      
  28.             View music_popunwindwow = mLayoutInflater.inflate(      
  29.                     R.layout.music_popwindow, null);      
  30.             PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, LayoutParams.FILL_PARENT,      
  31.                     LayoutParams.WRAP_CONTENT);      
  32.                   
  33.             mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.RIGHT|Gravity.BOTTOM, 0, 0);      
  34.         }      
  35.     }      
  36. }    

package com.android.tutor;<br />import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.view.Gravity;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.view.ViewGroup.LayoutParams;<br />import android.widget.Button;<br />import android.widget.PopupWindow;<br />public class PopupWindowDemo extends Activity implements OnClickListener{<br /> private Button btn; </p><p> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main); </p><p> btn = (Button)findViewById(R.id.btn);<br /> btn.setOnClickListener(this);<br /> }<br /> @Override<br /> public void onClick(View v) {<br /> Context mContext = PopupWindowDemo.this;<br /> if (v.getId() == R.id.btn) {<br /> LayoutInflater mLayoutInflater = (LayoutInflater) mContext<br /> .getSystemService(LAYOUT_INFLATER_SERVICE);<br /> View music_popunwindwow = mLayoutInflater.inflate(<br /> R.layout.music_popwindow, null);<br /> PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, LayoutParams.FILL_PARENT,<br /> LayoutParams.WRAP_CONTENT); </p><p> mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.RIGHT|Gravity.BOTTOM, 0, 0);<br /> }<br /> }<br />}  

 

需要強調的是這裡PopupWindow必須有某個事件觸發才會顯示出來,不然總會抱錯,不信大家可以試試!

隨著這個問題的出現,就會同學問了,那麼我想初始化讓PopupWindow顯示出來,那怎麼辦了,不去寄託於其他點擊事件,

在這裡我用了定時器Timer來實現這樣的效果,當然這裡就要用到Handler了,如果大家不理解的可以返回

 

view plaincopy to clipboardprint?
  1. Android 高手進階教程(九)之----Android Handler的使用!! 看一看,加深瞭解:  

Android 高手進階教程(九)之----Android Handler的使用!! 看一看,加深瞭解: 

 

下面是核心代碼:

 

view plaincopy to clipboardprint?
  1. package com.android.tutor;      
  2. import java.util.Timer;      
  3. import java.util.TimerTask;      
  4. import android.app.Activity;      
  5. import android.content.Context;      
  6. import android.os.Bundle;      
  7. import android.os.Handler;      
  8. import android.os.Message;      
  9. import android.view.Gravity;      
  10. import android.view.LayoutInflater;      
  11. import android.view.View;      
  12. import android.view.ViewGroup.LayoutParams;      
  13. import android.widget.PopupWindow;      
  14. public class PopupWindowDemo extends Activity{      
  15.     private Handler mHandler = new Handler(){      
  16.               
  17.         public void handleMessage(Message msg) {      
  18.             switch (msg.what) {      
  19.             case 1:      
  20.                 showPopupWindow();      
  21.                 break;      
  22.             }      
  23.         };      
  24.     };      
  25.           
  26.     public void onCreate(Bundle savedInstanceState) {      
  27.         super.onCreate(savedInstanceState);      
  28.         setContentView(R.layout.main);      
  29.               
  30.         //create the timer       
  31.         Timer timer = new Timer();      
  32.         timer.schedule(new initPopupWindow(), 100);      
  33.     }      
  34.           
  35.     private class initPopupWindow extends TimerTask{      
  36.         @Override     
  37.         public void run() {      
  38.                   
  39.             Message message = new Message();      
  40.             message.what = 1;      
  41.             mHandler.sendMessage(message);      
  42.                   
  43.         }             
  44.     }      
  45.           
  46.           
  47.     public void showPopupWindow() {      
  48.         Context mContext = PopupWindowDemo.this;      
  49.         LayoutInflater mLayoutInflater = (LayoutInflater) mContext      
  50.                 .getSystemService(LAYOUT_INFLATER_SERVICE);      
  51.         View music_popunwindwow = mLayoutInflater.inflate(      
  52.                 R.layout.music_popwindow, null);      
  53.         PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow,      
  54.                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);      
  55.         mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);      
  56.     }      
  57. }    

package com.android.tutor;<br />import java.util.Timer;<br />import java.util.TimerTask;<br />import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.os.Handler;<br />import android.os.Message;<br />import android.view.Gravity;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.ViewGroup.LayoutParams;<br />import android.widget.PopupWindow;<br />public class PopupWindowDemo extends Activity{<br /> private Handler mHandler = new Handler(){ </p><p> public void handleMessage(Message msg) {<br /> switch (msg.what) {<br /> case 1:<br /> showPopupWindow();<br /> break;<br /> }<br /> };<br /> }; </p><p> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main); </p><p> //create the timer<br /> Timer timer = new Timer();<br /> timer.schedule(new initPopupWindow(), 100);<br /> } </p><p> private class initPopupWindow extends TimerTask{<br /> @Override<br /> public void run() { </p><p> Message message = new Message();<br /> message.what = 1;<br /> mHandler.sendMessage(message); </p><p> }<br /> } </p><p> public void showPopupWindow() {<br /> Context mContext = PopupWindowDemo.this;<br /> LayoutInflater mLayoutInflater = (LayoutInflater) mContext<br /> .getSystemService(LAYOUT_INFLATER_SERVICE);<br /> View music_popunwindwow = mLayoutInflater.inflate(<br /> R.layout.music_popwindow, null);<br /> PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow,<br /> LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);<br /> mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);<br /> }<br />}  

 

效果如: 

這樣就可以初始化PopupWindow了,呵呵,這一節的布局檔案有點多,如果大家想要源碼的話,留下你們的Email,我會儘快發送給大家的,今天就到這裡,大家有什麼不明白的歡迎留言!!!謝謝~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.