【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android中怎么利用popupwindow顯示listview_第1頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android中怎么利用popupwindow顯示listview_第2頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android中怎么利用popupwindow顯示listview_第3頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余1頁(yè)可下載查看

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論