


下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android中怎么利用popupwindow顯示listview
這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么利用popupwindow顯示listview,文章內(nèi)容質(zhì)量較高,因此在下分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。1、創(chuàng)建一個(gè)popupwindow,并設(shè)置相應(yīng)的樣式。Java代碼private
void
popAwindow(View
parent)
{
if
(window
==
null)
{
LayoutInflater
lay
=
(LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View
v
=
lay.inflate(R.layout.popupwindow,
null);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_view));
//初始化按鈕
submit
=
(Button)
v.findViewById(R.id.submit);
submit.setOnClickListener(submitListener);
cancel
=
(Button)
v.findViewById(R.id.cancel);
cancel.setOnClickListener(cancelListener);
//初始化listview,加載數(shù)據(jù)。
list=(ListView)v.findViewById(R.id.lv);
MyAdapter
adapter=new
MyAdapter(Main.this);
list.setAdapter(adapter);
list.setItemsCanFocus(false);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setOnItemClickListener(listClickListener);
window
=
new
PopupWindow(v,
500,260);
}
//設(shè)置整個(gè)popupwindow的樣式。
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));
//使窗口里面的空間顯示其相應(yīng)的效果,比較點(diǎn)擊button時(shí)背景顏色改變。
//如果為false點(diǎn)擊相關(guān)的空間表面上沒有反應(yīng),但事件是可以監(jiān)聽到的。
//listview的話就沒有了作用。
window.setFocusable(true);
window.update();
window.showAtLocation(parent,
Gravity.CENTER_VERTICAL,
0,
0);
}
OnItemClickListener
listClickListener
=
new
OnItemClickListener()
{
@Override
public
void
onItemClick(AdapterView<?>
parent,
View
view,
int
position,
long
id)
{
ViewHolder
vHollder
=
(ViewHolder)
view.getTag();
//
在每次獲取點(diǎn)擊的item時(shí)將對于的checkbox狀態(tài)改變,同時(shí)修改map的值。
vHollder.cBox.toggle();
MyAdapter.isSelected.put(position,
vHollder.cBox.isChecked());
}
};給按鈕添加監(jiān)聽事件:Java代碼OnClickListener
submitListener
=
new
OnClickListener()
{
@Override
public
void
onClick(View
v)
{
//這兒可以寫提交數(shù)據(jù)的代碼。
closeWindow();
}
};
OnClickListener
cancelListener=new
OnClickListener(){
@Override
public
void
onClick(View
v){
closeWindow();
}
};
private
void
closeWindow(){
//將每個(gè)checkbox的標(biāo)記改為false,以便下次彈出window時(shí)是初始的狀態(tài)。
for
(int
j
=
0;
j
<
MyAdapter.isSelected.size();
j++)
{
MyAdapter.isSelected.put(j,
false);
ViewHolder
vHollder
=
(ViewHolder)
list.getChildAt(j).getTag();
vHollder.cBox.setChecked(false);
}
//提交數(shù)據(jù)時(shí)關(guān)閉popupwindow。
if
(window
!=
null)
{
window.dismiss();
}
}在layout中新建popupwindow.xml文件,具體內(nèi)容如下,主要是對window的布局:Java代碼<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18dip"
android:background="@drawable/rounded_corners_list"
android:text="這是一個(gè)popupWindow的例子"/>
<!--
如果layout_width的值為fill_parent時(shí),居中要用android:gravity="center"-->
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners_list"/>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners_list">
<Button
android:id="@+id/submit"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="提交"/>
<Button
android:id="@+id/cancel"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="取消"/>
</LinearLayout>
</LinearLayout>新建rounded_corners_pop.xml,用于自定義窗口的樣式文件,具體內(nèi)容如下:Java代碼
<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android">
<solid
android:color="#ffffffff"
/>
<stroke
android:width="3dp"
color="#ffff8080"
/>
<corners
android:radius="10dp"
/>
<padding
android:left="3dp"
android:top="3dp"
android:right="3dp"
android:bottom="3dp"
/>
</shape>這個(gè)就可以實(shí)現(xiàn)圓角的樣式,周圍的白邊是通過在白的樣式上面疊加黑色的來實(shí)現(xiàn)的。其他樣式文件大家可以參考上面的rounded_corners_pop.xml自己寫。2、在main.xml中添加按鈕,一個(gè)用于顯示window,一個(gè)用于隱藏windowJava代碼
<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/lmain"
>
<Button
android:id="@+id/myButton1"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="顯示"/>
<Button
androi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 華為新員工入職培訓(xùn)
- 2025陶瓷采購協(xié)議 陶瓷銷售合同
- 2025飲料供貨合同范本
- 2023九年級數(shù)學(xué)下冊 第1章 二次函數(shù)1.2 二次函數(shù)的圖象與性質(zhì)第5課時(shí) 二次函數(shù)y=ax2+bx+c(a≠0)的圖象與性質(zhì)教學(xué)實(shí)錄 (新版)湘教版
- 《衛(wèi)星運(yùn)行時(shí)間》(教學(xué)設(shè)計(jì))-2024-2025學(xué)年四年級上冊數(shù)學(xué)北師大版001
- 2025商業(yè)采購合同范本全文
- 第2課 讓海龜畫圖(教案)2023-2024學(xué)年六年級上冊信息技術(shù)人教版
- 員工激勵(lì)方法培訓(xùn)
- 2025專業(yè)版中華人民共和國合同法解釋與分析
- 培育耐心資本推動(dòng)產(chǎn)業(yè)創(chuàng)新心得體會(huì)發(fā)言
- 兒童發(fā)展問題的咨詢與輔導(dǎo)-案例1-5-國開-參考資料
- 倉儲(chǔ)場所消防安全培訓(xùn)
- 大學(xué)課件-電路分析基礎(chǔ)
- 2025年中國流行成分和原料消費(fèi)深度洞察白皮書
- 2025年昆明長水機(jī)場勤務(wù)員招聘筆試參考題庫含答案解析
- DG-TJ 08-2336-2020 綠道建設(shè)技術(shù)標(biāo)準(zhǔn)
- 安全生產(chǎn)法律法規(guī)匯編(2025版)
- 《光電對抗原理與應(yīng)用》課件第3章
- 二次供水水箱清洗操作流程
- AEO貿(mào)易安全培訓(xùn)
- 推行注塑生產(chǎn)自動(dòng)化改造計(jì)劃
評論
0/150
提交評論