


下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android中怎么利用popupwindow顯示listview
這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么利用popupwindow顯示listview,文章內(nèi)容質(zhì)量較高,因此在下分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。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í)將對(duì)于的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)容如下,主要是對(duì)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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024-2025學(xué)年高二化學(xué)人教版選擇性必修3教學(xué)課件 第一章 第一節(jié) 第2課時(shí) 有機(jī)化合物中的共價(jià)鍵
- 認(rèn)識(shí)未來生活之美中英語言文化交流教學(xué)教案
- 三農(nóng)村居民文化生活豐富化方案
- 環(huán)保行業(yè)工業(yè)廢水處理與循環(huán)利用技術(shù)方案
- 品牌排行榜表格版
- 企業(yè)并購(gòu)保密協(xié)議條款補(bǔ)充協(xié)議
- 云計(jì)算服務(wù)協(xié)議文檔
- 行政后勤工作指導(dǎo)手冊(cè)
- 中醫(yī)藥現(xiàn)代化種植與管理技術(shù)支持計(jì)劃
- 藝術(shù)類學(xué)生作品賞析
- GB 45184-2024眼視光產(chǎn)品元件安全技術(shù)規(guī)范
- 2025年湖南科技職業(yè)學(xué)院高職單招數(shù)學(xué)歷年(2016-2024)頻考點(diǎn)試題含答案解析
- 2025年新人教版八年級(jí)下冊(cè)物理全冊(cè)教案
- 《建筑電氣設(shè)計(jì)》課件
- 品管圈PDCA案例-介入中心提高手術(shù)患者交接記錄書寫合格率醫(yī)院品質(zhì)管理成果匯報(bào)
- 第十七屆山東省職業(yè)院校技能大賽中職組“西式烹飪”賽項(xiàng)規(guī)程
- 華東師范大學(xué)《外國(guó)人文經(jīng)典(下)》2022-2023學(xué)年第一學(xué)期期末試卷
- 儲(chǔ)能電池模組PACK和系統(tǒng)集成項(xiàng)目可行性研究報(bào)告
- 2024年安徽省公務(wù)員錄用考試《行測(cè)》真題及解析
- 牙慢性損傷-楔狀缺損
- JTJ034-2000 公路路面基層施工技術(shù)規(guī)范
評(píng)論
0/150
提交評(píng)論