Android下拉框PopupWindow使用詳解
本文實(shí)例為大家分享了Android下拉框PopupWindow展示的具體代碼,供大家參考,具體內(nèi)容如下

activity_main.xml布局
<RelativeLayout 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" tools:context="com.example.spin.MainActivity" > <EditText android:id="@+id/et_editText" android:layout_width="250dp" android:layout_height="50dp" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/hello_world" /> <ImageView android:id="@+id/down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/down_arrow" android:layout_alignRight="@id/et_editText" android:layout_marginTop="20dp" android:clickable="true"/> </RelativeLayout>
List_item.xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/user"/> <TextView android:id="@+id/tv_list_item" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="100000000"/> <ImageView android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/delete"/> </LinearLayout>
代碼實(shí)現(xiàn)
public class MainActivity extends ActionBarActivity {
private EditText et_editText;//編輯框
private ImageView down;//下拉按鈕
private ListView listView;
private List<String> numList;
private PopupWindow popWin;
private Boolean isDown=false;//判斷彈窗是否顯示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_editText = (EditText) findViewById(R.id.et_editText);
down = (ImageView) findViewById(R.id.down);
//創(chuàng)建集合 儲存號碼
numList = new ArrayList<String>();
for(int i=0;i<20;i++){
numList.add("100000000"+i);
}
initListView();
//對下拉按鈕設(shè)置監(jiān)聽 當(dāng)進(jìn)行點(diǎn)擊時(shí) 彈出popWin
down.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!isDown){
//定義一個(gè)popupWindow
popWin=new PopupWindow(MainActivity.this);
popWin.setWidth(et_editText.getWidth());//設(shè)置寬度 和編輯框的寬度相同
popWin.setHeight(200); //設(shè)置高度
//為popWin填充內(nèi)容
popWin.setContentView(listView);
//點(diǎn)擊popWin區(qū)域之外 自動關(guān)閉popWin
popWin.setOutsideTouchable(true);
/**
* 設(shè)置彈出窗口顯示的位置
* 參數(shù)一:相對于參數(shù)的位置進(jìn)行顯示 即在編輯框的下面顯示
* 參數(shù)二 三:x y軸的偏移量
*/
popWin.showAsDropDown(et_editText, 0, 0);
isDown=true;
}else{
popWin.dismiss();
isDown=false;
}
}
});
}
//點(diǎn)擊返回按鈕
@Override
public void onBackPressed() {
/**
* 當(dāng)用戶點(diǎn)擊返回按鈕時(shí) 是整個(gè)activity退出 而且給人的感覺是直接退出 窗口可能還是顯示狀態(tài),
為了避免內(nèi)存泄露,先關(guān)閉彈窗
*
* 當(dāng)點(diǎn)擊返回按鈕時(shí) 如果窗口存在且正在顯示 則關(guān)閉窗口
*/
if(popWin!=null&&popWin.isShowing()){
popWin.dismiss();
}
super.onBackPressed();
}
private void initListView() {
listView = new ListView(this);
//設(shè)置listView的背景
listView.setBackgroundResource(R.drawable.listview_background);
//設(shè)置條目之間的分割線及滾動條不可見
listView.setDivider(null);
listView.setVerticalScrollBarEnabled(false);
//設(shè)置適配器
listView.setAdapter(new MyListAdapter());
}
private class MyListAdapter extends BaseAdapter{
@Override
public int getCount() {
return numList==null?0:numList.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=View.inflate(getApplicationContext(), R.layout.list_item, null);
holder=new ViewHolder();
holder.tvNum=(TextView) convertView.findViewById(R.id.tv_list_item);
holder.delete=(ImageView) convertView.findViewById(R.id.delete);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.tvNum.setText(numList.get(position));
//對刪除按鈕設(shè)置監(jiān)聽事件
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//當(dāng)點(diǎn)擊刪除按鈕時(shí) 刪除條目
numList.remove(position);
//刷新ListView
MyListAdapter.this.notifyDataSetChanged();
}
});
//對條目設(shè)置監(jiān)聽事件 點(diǎn)擊條目后 將num設(shè)置到編輯框中
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//點(diǎn)擊條目后 將num設(shè)置到編輯框中
et_editText.setText(numList.get(position));
popWin.dismiss();
}
});
return convertView;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
private class ViewHolder {
TextView tvNum;
ImageView delete;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Github簡單易用的?Android?ViewModel?Retrofit框架
這篇文章主要介紹了Github簡單易用的Android?ViewModel?Retrofit框架,RequestViewMode有自動對LiveData進(jìn)行緩存管理,每個(gè)retrofit api接口復(fù)用一個(gè)livedata的優(yōu)勢。下文具體詳情,感興趣的小伙伴可以參考一下2022-06-06
Android手機(jī)信號強(qiáng)度檢測詳細(xì)介紹
這篇文章主要介紹了Android手機(jī)信號強(qiáng)度檢測的相關(guān)資料,android定義了2種信號單位:dBm和asu。具體兩種的關(guān)系本文給大家介紹非常詳細(xì),需要的朋友可以參考下2016-11-11
Android學(xué)習(xí)筆記(一)環(huán)境安裝及第一個(gè)hello world
最近在學(xué)習(xí)安卓開發(fā),記錄下環(huán)境安裝和第一個(gè)hello world的誕生過程,希望對大家有所幫助2014-07-07
Android仿硅谷商城實(shí)現(xiàn)購物車實(shí)例代碼
這篇文章主要介紹了Android購物車編輯實(shí)現(xiàn),小編覺得挺不錯的,一起跟隨小編過來看看吧2018-05-05
Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動漸變效果的詳細(xì)代碼
這篇文章主要介紹了Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動漸變效果的詳細(xì)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Kotlin語言使用BroadcastReceiver示例介紹
Android開發(fā)的四大組件分別是:活動(activity),用于表現(xiàn)功能;服務(wù)(service),后臺運(yùn)行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個(gè)應(yīng)用中存儲和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫,本篇著重介紹廣播組件2022-09-09
Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼(附源碼)
這篇文章主要介紹了Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android基于OpenCV實(shí)現(xiàn)圖像金字塔
圖像金字塔是圖像中多尺度表達(dá)的一種,最主要用于圖像的分割,是一種以多分辨率來解釋圖像的有效但概念簡單的結(jié)構(gòu)。本文講解Android基于OpenCV實(shí)現(xiàn)圖像金字塔的步驟2021-06-06
Android通過實(shí)現(xiàn)GridView的橫向滾動實(shí)現(xiàn)仿京東秒殺效果
這篇文章主要介紹了Android通過實(shí)現(xiàn)GridView的橫向滾動實(shí)現(xiàn)仿京東秒殺效果,實(shí)現(xiàn)代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07

