chap4-菜單與對話框_第1頁
chap4-菜單與對話框_第2頁
chap4-菜單與對話框_第3頁
chap4-菜單與對話框_第4頁
chap4-菜單與對話框_第5頁
已閱讀5頁,還剩33頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

菜單與對話框

4.1任務(wù)1-使用選項(xiàng)菜單OptionsMenu21菜單的XML文件2選項(xiàng)菜單的創(chuàng)建方法3選項(xiàng)菜單的事件響應(yīng)方法OptionsMenuOptionsMenu不依賴于內(nèi)容,在應(yīng)用創(chuàng)建視圖時,OptionsMenu也被生成,常用于全局場合項(xiàng)目模板創(chuàng)建一個空白活動頁面時,默認(rèn)沒有菜單文件夾,在創(chuàng)建OptionsMenu時,可利用向?qū)?chuàng)建相關(guān)文件夾和文件重要方法onCreateOptionsMenu()應(yīng)用首次生成選項(xiàng)菜單時調(diào)用,之后不再被調(diào)用,適合生成靜態(tài)菜單onPrepareOptionsMenu()沒次點(diǎn)擊選項(xiàng)菜單時被調(diào)用,適合生成動態(tài)內(nèi)容的菜單項(xiàng)onOptionsItemSelected()選項(xiàng)菜單的點(diǎn)擊事件回調(diào)菜單XMLmenu根節(jié)點(diǎn)item節(jié)點(diǎn)為菜單項(xiàng)android:title菜單項(xiàng)在菜單中所顯示的文本android:icon菜單項(xiàng)的圖標(biāo),須與app:showAsAction配合app:showAsAction屬性always:在動作欄以圖標(biāo)形式顯示菜單項(xiàng)3相關(guān)布局文件4<!--OptionsMenu布局文件res/menu/opt_menu.xml-->

<menuxmlns:app="/apk/res-auto"

xmlns:android="/apk/res/android">

<item

android:id="@+id/opt_add"

android:icon="@drawable/ic_baseline_add_box_24"

android:title="增加"

app:showAsAction="always"/>

<item

android:id="@+id/opt_modify"

android:title="修改"/>

<item

android:id="@+id/opt_delete"

android:title="刪除"/>

</menu><vectorandroid:height="24dp"android:tint="#FFFFFF"

android:viewportHeight="24"android:viewportWidth="24"

android:width="24dp"xmlns:android="/apk/res/android">

<pathandroid:fillColor="#FF9800"android:pathData="M19,3L5,3c-1.11,0-2,0.9-2,2v14c0,1.10.89,22,2h14c1.1,02,-0.92,-2L21,5c0,-1.1-0.9,-2-2,-2zM17,13h-4v4h-2v-4L7,13v-2h4L11,7h2v4h4v2z"/>

</vector><!--my_main.xml-->

<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text=""/>

</LinearLayout>矢量圖文件,右擊res/drawable文件夾,new→VectorAsset選項(xiàng)創(chuàng)建實(shí)現(xiàn)MainActivity5publicclassMainActivityextendsAppCompatActivity{

TextViewtv;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

tv=findViewById(R.id.tv_result);

}

@Override

publicbooleanonCreateOptionsMenu(Menumenu){

getMenuInflater().inflate(R.menu.opt_menu,menu);

//將res/menu/opt_menu.xml菜單文件填充到menu對象上

//newMenuInflater(getApplicationContext()).inflate(R.menu.opt_menu,menu);

//也可以使用MenuInflater填充菜單

returnsuper.onCreateOptionsMenu(menu);

}

@Override

publicbooleanonOptionsItemSelected(@NonNullMenuItemitem){

Strings="";

//哪個選項(xiàng)菜單項(xiàng)被選中,建議使用菜單項(xiàng)的id進(jìn)行判斷

switch(item.getItemId()){

caseR.id.opt_add:

s="增加";

break;

caseR.id.opt_modify:

s="修改";

break;

caseR.id.opt_delete:

s="刪除";

break;

}

tv.setText(s);

returnsuper.onOptionsItemSelected(item);

}

}4.2任務(wù)2-使用上下文菜單ContextMenu61注冊上下文菜單2上下文菜單的創(chuàng)建和點(diǎn)擊響應(yīng)方法3上下文菜單獲取觸發(fā)位置4適配器數(shù)據(jù)的更新ContextMenuContextMenu依賴于內(nèi)容,需要注冊才能被使用調(diào)用registerForContextMenu()方法對UI對象注冊ContextMenu長按被注冊的視圖,彈出上下文菜單重要方法onCreateContextMenu()每次彈出上下文菜單均會被調(diào)用onContextItemSelected(MenuItemitem)菜單項(xiàng)點(diǎn)擊事件回調(diào)若是針對ListView、GridView等組件,需要知道彈出上下文菜單的位置item.getMenuInfo()方法得到ContextMenuInfo對象無法得到position或者id信息須將ContextMenuInfo對象強(qiáng)制類型轉(zhuǎn)換成AdapterContextMenuInfo對象適配器視圖更新方法notifyDataSetChanged()方法一旦數(shù)據(jù)源的數(shù)據(jù)發(fā)生變化,需要調(diào)用適配器更新視圖,保持視圖與數(shù)據(jù)的一致性7項(xiàng)目的布局文件8<!--ContextMenu布局文件res/menu/ctx_menu.xml-->

<menuxmlns:app="/apk/res-auto"

xmlns:android="/apk/res/android">

<item

android:id="@+id/ctx_insert"

android:title="插入"/>

<item

android:id="@+id/ctx_delete"

android:title="刪除"/>

</menu><!--MainActivity的布局文件my_main.xml-->

<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<ListView

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</LinearLayout>實(shí)現(xiàn)MainActivity9publicclassMainActivityextendsAppCompatActivity{

ArrayList<String>list=newArrayList<>();

ArrayAdapter<String>adapter;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

for(inti=0;i<20;i++){

Strings=String.format("Item%02d",i);

list.add(s);

}

adapter=newArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);

ListViewlv=findViewById(R.id.listView);

lv.setAdapter(adapter);

registerForContextMenu(lv);

//對ListView視圖注冊ContextMenu,長按ListView視圖會彈出ContextMenu

//若不注冊ContextMenu,則不會生成對應(yīng)菜單

}

@Override

publicvoidonCreateContextMenu(ContextMenumenu,Viewv,

ContextMenu.ContextMenuInfomenuInfo){

super.onCreateContextMenu(menu,v,menuInfo);

getMenuInflater().inflate(R.menu.ctx_menu,menu);

//將res/menu/ctx_menu.xml菜單生成到菜單對象menu上

}

@Override

publicbooleanonContextItemSelected(@NonNullMenuItemitem){

ContextMenu.ContextMenuInfomenuInfo=item.getMenuInfo();

//通過item獲得菜單項(xiàng)對象ContextMenuInfo

AdapterView.AdapterContextMenuInfoadapterContextMenuInfo=

(AdapterView.AdapterContextMenuInfo)menuInfo;

//將ContextMenuInfo對象強(qiáng)制類型轉(zhuǎn)換為AdapterContextMenuInfo

//通過AdapterContextMenuInfo獲得位置信息position

intposition=adapterContextMenuInfo.position;

switch(item.getItemId()){

caseR.id.ctx_insert:

Strings=String.format("Random:%d",newRandom().nextInt(1000));

//產(chǎn)生一個區(qū)間為[0,1000)的隨機(jī)整數(shù)(不包括1000)

list.add(position,s);//數(shù)據(jù)源插入數(shù)據(jù)s至指定位置position

adapter.notifyDataSetChanged();

//數(shù)據(jù)源發(fā)生變化,通知適配器數(shù)據(jù)更新,適配器全盤更新

break;

caseR.id.ctx_delete:

list.remove(position);//數(shù)據(jù)源刪除指定位置position的數(shù)據(jù)

adapter.notifyDataSetChanged();//通知適配器更新視圖

break;

}

returnsuper.onContextItemSelected(item);

}

}4.3任務(wù)3-使用彈出菜單PopupMenu101上下文菜單的局限性2PopupMenu可以實(shí)現(xiàn)多視圖注冊3PopupMenu的使用方法項(xiàng)目的布局文件11<!--TextView的菜單文件res/menu/ctx_tv_menu.xml-->

<menuxmlns:app="/apk/res-auto"

xmlns:android="/apk/res/android">

<item

android:id="@+id/ctx_tv_12"

android:title="12sp"/>

<item

android:id="@+id/ctx_tv_16"

android:title="16sp"/>

</menu><!--ContextMenu布局文件res/menu/ctx_menu.xml-->

<menuxmlns:app="/apk/res-auto"

xmlns:android="/apk/res/android">

<item

android:id="@+id/ctx_insert"

android:title="插入"/>

<item

android:id="@+id/ctx_delete"

android:title="刪除"/>

</menu><!--MainActivity的布局文件my_main.xml-->

<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<ListView

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</LinearLayout>基于上下文菜單的雙視圖注冊12publicclassMainActivityextendsAppCompatActivity{

ArrayList<String>list=newArrayList<>();

ArrayAdapter<String>adapter;

ListViewlv;

TextViewtv;

intctx_position=0;//設(shè)置ContextMenu觸發(fā)位置的變量為全局變量

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

for(inti=0;i<20;i++){

Strings=String.format("Item%02d",i);

list.add(s);

}

adapter=newArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);

lv=findViewById(R.id.listView);

lv.setAdapter(adapter);

registerForContextMenu(lv);//對ListView視圖注冊ContextMenu

tv=findViewById(R.id.tv_result);

registerForContextMenu(tv);//對TextView視圖注冊ContextMenu

}

@Override

publicvoidonCreateContextMenu(ContextMenumenu,Viewv,

ContextMenu.ContextMenuInfomenuInfo){

super.onCreateContextMenu(menu,v,menuInfo);

if(v==lv){

//若是ListView,生成對應(yīng)菜單,并獲得位置信息賦給ctx_position

getMenuInflater().inflate(R.menu.ctx_menu,menu);

AdapterView.AdapterContextMenuInfoadapterContextMenuInfo=

(AdapterView.AdapterContextMenuInfo)menuInfo;

ctx_position=adapterContextMenuInfo.position;

}elseif(v==tv){

//若是TextView,生成另一種菜單

getMenuInflater().inflate(R.menu.ctx_tv_menu,menu);

}

}

@Override

publicbooleanonContextItemSelected(@NonNullMenuItemitem){

//TextView觸發(fā)的ContextMenu對象對應(yīng)的menuInfo是null,無position屬性

intposition=ctx_position;

//ctx_position在onCreateContextMenu()方法中被賦值

switch(item.getItemId()){

caseR.id.ctx_insert:

Strings=String.format("Random:%d",newRandom().nextInt(1000));

list.add(position,s);

adapter.notifyDataSetChanged();

break;

caseR.id.ctx_delete:

list.remove(position);

adapter.notifyDataSetChanged();

caseR.id.ctx_tv_12:

tv.setTextSize(12.0f);//設(shè)置文本字體大小

break;

caseR.id.ctx_tv_16:

tv.setTextSize(16.0f);

break;

}

returnsuper.onContextItemSelected(item);

}

}PopupMenu使用要點(diǎn)PopupMenu的構(gòu)造方法publicPopupMenu(android.content.Contextcontext,android.view.Viewanchor)context為上下文,可通過傳遞Activity對象或者直接通過getApplicationContext()方法獲得anchor為PopupMenu所依賴的視圖,即可理解為PopupMenu所需要注冊的視圖構(gòu)造出PopupMenu的對象popupMenu,使用popupMenu實(shí)現(xiàn)菜單填充和事件處理菜單填充popupMenu.getMenuInflater()方法得到菜單填充器,進(jìn)而通過菜單填充器的inflate()方法填充菜單popupMenu.getMenu()方法獲得Menu對象需要調(diào)用show()方法進(jìn)行顯示事件處理PopupMenu對象調(diào)用setOnMenuItemClickListener()方法設(shè)置偵聽器onMenuItemClick()回調(diào)方法中進(jìn)行處理PopupMenu可以對不同視圖分別注冊,得到不同的PopupMenu對象,并分別對之進(jìn)行菜單填充和事件偵聽處理,提高代碼解耦性13基于PopupMenu的實(shí)現(xiàn)14publicclassMainActivityextendsAppCompatActivity{

ArrayList<String>list=newArrayList<>();

ArrayAdapter<String>adapter;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

for(inti=0;i<20;i++){

Strings=String.format("Item%02d",i);

list.add(s);

}

adapter=newArrayAdapter<>(this,

android.R.layout.simple_list_item_1,list);

ListViewlv=findViewById(R.id.listView);

lv.setAdapter(adapter);

registerForContextMenu(lv);//對ListView視圖注冊ContextMenu

TextViewtv=findViewById(R.id.tv_result);

tv.setOnLongClickListener(newView.OnLongClickListener(){

//利用對象tv長按彈出PopupMenu

@Override

publicbooleanonLongClick(Viewv){

showTextViewPopUpMenu(tv);//自定義方法,顯示PopupMenu

returntrue;//true表示長按事件不再往下傳,false則繼續(xù)往下傳遞事件

}

});

}

privatevoidshowTextViewPopUpMenu(TextViewv){

PopupMenupopupMenu=newPopupMenu(getApplicationContext(),v);

//對視圖v生成菜單對象popupMenu

popupMenu.getMenuInflater()

.inflate(R.menu.ctx_tv_menu,popupMenu.getMenu());

//利用菜單對象popupMenu的getMenuInflater()方法獲得對應(yīng)菜單填充器

//利用菜單對象popupMenu的getMenu()方法獲得Menu對象menu

popupMenu.setOnMenuItemClickListener(

newPopupMenu.OnMenuItemClickListener(){

//對popupMenu設(shè)置菜單項(xiàng)點(diǎn)擊偵聽

@Override

publicbooleanonMenuItemClick(MenuItemitem){

switch(item.getItemId()){

caseR.id.ctx_tv_12:

v.setTextSize(12.0f);

break;

caseR.id.ctx_tv_16:

v.setTextSize(16.0f);

break;

}

returntrue;//true表示菜單項(xiàng)選中事件不再往下傳,false則繼續(xù)下傳

}

});

popupMenu.show();//調(diào)用show()方法顯示PopupMenu

}

@Override

publicvoidonCreateContextMenu(ContextMenumenu,Viewv,

ContextMenu.ContextMenuInfomenuInfo){

}

@Override

publicbooleanonContextItemSelected(@NonNullMenuItemitem){

}

}4.4任務(wù)4-使用對話框AlertDialog151消息對話框2列表對話框3單選對話框?qū)υ捒駻lertDialog的使用要點(diǎn)AlertDialog使用AlertDialog.Builder()方法創(chuàng)建1個Builder對象setTitle()方法設(shè)置對話框標(biāo)題setMessage()方法設(shè)置對話框的文本內(nèi)容setItems()方法設(shè)置列表數(shù)據(jù)和點(diǎn)擊偵聽setSingleChoiceItems()方法設(shè)置單選數(shù)據(jù)、默認(rèn)選中位置和點(diǎn)擊偵聽setView()方法設(shè)置自定義視圖,此時建議使用LayoutInflater從布局文件中生成視圖對象setMultiChoiceItems()方法設(shè)置多選內(nèi)容、多選默認(rèn)選中的數(shù)據(jù)和相關(guān)偵聽事件setAdapter()方法設(shè)置適配器setCursor()方法設(shè)置Cursor數(shù)據(jù),Cursor為數(shù)據(jù)庫查詢返回的游標(biāo)setIcon()方法設(shè)置圖標(biāo)show()方法顯示對話框?qū)υ捒虻陌粹o設(shè)置方法setPositiveButton()方法設(shè)置確定按鈕setNegativeButton()方法設(shè)置取消按鈕setNeutralButton()方法設(shè)置中間按鈕第1個參數(shù)是按鈕所顯示的文本第2個參數(shù)是對應(yīng)的點(diǎn)擊事件接口,若不需要處理點(diǎn)擊事件,可傳入null16項(xiàng)目的布局文件my_main.xml17<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text=""/>

<Button

android:id="@+id/bt1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="消息對話框"/>

<Button

android:id="@+id/bt2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="列表對話框"/>

<Button

android:id="@+id/bt3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="單選對話框"/>

</LinearLayout>實(shí)現(xiàn)MainActivity18publicclassMainActivityextendsAppCompatActivity{

TextViewtv;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

tv=findViewById(R.id.tv_result);

String[]hobbies=newString[]{"游泳","跑步","籃球"};

findViewById(R.id.bt1).setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

showMessageDialog("這是一個消息提示框");

}

});

findViewById(R.id.bt2).setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

showItemsDialog(hobbies);

}

});

findViewById(R.id.bt3).setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

showSingleChoiceDialog(hobbies);

}

});

}

privatevoidshowMessageDialog(Strings){

AlertDialog.Builderbl=newAlertDialog.Builder(this);

bl.setTitle("消息提示框").setMessage(s);

bl.setNegativeButton("退出",null);//退出按鈕,null代表不對事件響應(yīng)做處理

bl.show();

}

privatevoidshowItemsDialog(String[]hobbies){

AlertDialog.Builderbl=newAlertDialog.Builder(this);

bl.setTitle("列表框").setItems(hobbies,

newDialogInterface.OnClickListener(){

@Override

publicvoidonClick(DialogInterfacedialog,intwhich){

//每次點(diǎn)擊列表項(xiàng)均會更新TextView

Stringhobby=hobbies[which];

//若要在PositiveButton點(diǎn)擊事件中獲得hobby

//不借助全局變量或者自定義類,難以實(shí)現(xiàn)

tv.setText(hobby);

}

});

bl.setNegativeButton("退出",null);

bl.show();

}實(shí)現(xiàn)MainActivity(續(xù))19

privatevoidshowSingleChoiceDialog(String[]hobbies){

//finalintchoiceIndex=0;//無法實(shí)現(xiàn),final變量不能被二次賦值

//finalIntegerchoiceIndex=newInteger(0);

//無法實(shí)現(xiàn),Integer類沒有setter相關(guān)方法

finalMyIntegermyInteger=newMyInteger(0);

//不加final無法在匿名回調(diào)方法中被訪問

//通過自定義類解決int類型的final變量不能二次賦值問題

//myInteger不能重新指向new對象,但可以調(diào)用類方法對類成員改值

AlertDialog.Builderbl=newAlertDialog.Builder(this);

bl.setTitle("單選對話框");

bl.setSingleChoiceItems(hobbies,myInteger.getPosition(),

newDialogInterface.OnClickListener(){

@Override

publicvoidonClick(DialogInterfacedialog,intwhich){

//showMessageDialog("inside");//測試對話框中是否能生成另一個對話框

myInteger.setPosition(which);

//通過setPosition()方法給類成員變量重新賦值

}

});

bl.setPositiveButton("確定",newDialogInterface.OnClickListener(){

//點(diǎn)擊確定按鈕,在回調(diào)中處理確定按鈕對應(yīng)的邏輯,更新愛好選項(xiàng)到TextView

@Override

publicvoidonClick(DialogInterfacedialog,intwhich){

//從自定義類對象myInteger中取選中索引值

intposition=myInteger.getPosition();

tv.setText(hobbies[position]);

}

});

bl.setNegativeButton("退出",null);

bl.show();

}

classMyInteger{

//定義一個內(nèi)部類解決final問題

//類對象變成final,不能重新指向?qū)ο?,但是可以調(diào)用類的相

//關(guān)方法改變類成員變量的值

privateintposition=0;

publicMyInteger(intposition){

this.position=position;

}

publicintgetPosition(){

returnposition;

}

publicvoidsetPosition(intposition){

this.position=position;

}

}

}通過自定義類實(shí)現(xiàn)final變量的賦值問題4.5任務(wù)5-使用自定義視圖對話框201自定義視圖的使用方法2利用自定義接口提高代碼的解耦性3使用對話框新增修改數(shù)據(jù)項(xiàng)目的布局文件21<!--MainActivity的布局文件my_main.xml-->

<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="YournameandID"/>

<ListView

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</LinearLayout><!--ContextMenu菜單文件res/menu/ctx_menu.xml-->

<menuxmlns:android="/apk/res/android"

xmlns:app="/apk/res-auto">

<item

android:id="@+id/ctx_add"

android:title="新增"/>

<item

android:id="@+id/ctx_edit"

android:title="修改"/>

</menu><!--AlertDialog自定義視圖res/layout/dialog_view.xml-->

<LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<EditText

android:id="@+id/dialog_et_name"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="Inputyourname"

android:text=""/>

<EditText

android:id="@+id/dialog_et_phone"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="Inputyourphonenumber"

android:text=""/>

</LinearLayout>數(shù)據(jù)封裝類PhoneData22publicclassPhoneData{

privateStringname;

privateStringphone;

publicPhoneData(Stringname,Stringphone){

this.name=name;

this.phone=phone;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetPhone(){

returnphone;

}

publicvoidsetPhone(Stringphone){

this.phone=phone;

}

@Override

publicStringtoString(){

//必須改寫toString()方法,否則無法將PhoneData字符串化顯示在適配器中

returnString.format("%s%s",name,phone);

}

}未改寫toString(),ArrayAdapter顯示效果實(shí)現(xiàn)自定義視圖對話框PhoneDataDialog23publicclassPhoneDataDialog{

privateContextcontext;//LayoutInflater需要Context參數(shù)

privateStringtitle;//對話框標(biāo)題

publicinterfaceOnSubmitListener{//自定義一個接口

voidonSubmit(PhoneDataupdatedData);

//通過用戶實(shí)現(xiàn)接口的onSubmit()方法對updatedData進(jìn)行處理

//使用updatedData修改數(shù)據(jù)或者新增數(shù)據(jù)完全取決于用戶實(shí)現(xiàn)的代碼

//接口回調(diào)在對話框的確定按鈕中觸發(fā)

}

publicPhoneDataDialog(Contextcontext,Stringtitle){

this.context=context;

this.title=title;

}

publicvoidshowDialog(PhoneDatadata,OnSubmitListenerl){

/***

data數(shù)據(jù)是用戶傳進(jìn)來的待修改數(shù)據(jù),若是新增數(shù)據(jù),可傳null

OnSubmitListener是用戶實(shí)現(xiàn)的接口,在確定按鈕中觸發(fā)回調(diào),

通過onSubmit(PhoneDataupdatedData)傳出修改后的數(shù)據(jù)updatedData

用戶則在調(diào)用showDialog()時實(shí)現(xiàn)回調(diào)方法,對updatedData處理(新增或者修改)

*/

PhoneDatatemp=newPhoneData("","");

if(data!=null){//若data不為空,則將值賦給變量temp

temp.setName(data.getName());

temp.setPhone(data.getPhone());

}

Viewv=LayoutInflater.from(context)

.inflate(R.layout.dialog_view,null,false);

//將布局文件填充為視圖對象v

EditTextet_name=v.findViewById(R.id.dialog_et_name);

EditTextet_phone=v.findViewById(R.id.dialog_et_phone);

//對兩個EditText賦data傳進(jìn)的初值

et_name.setText(temp.getName());

et_phone.setText(temp.getPhone());

AlertDialog.Builderbl=newAlertDialog.Builder(context);

bl.setTitle(title);

bl.setView(v);//將XML布局填充的視圖對象v設(shè)置為對話框的內(nèi)容

bl.setNegativeButton("取消",null);

bl.setPositiveButton("確定",newDialogInterface.OnClickListener(){

@Override

publicvoidonClick(DialogInterfacedialog,intwhich){

PhoneDataupdatedData=newPhoneData(

et_name.getText().toString(),

et_phone.getText().toString());

//不直接修改變量data

//使用updatedData的目的是data可能是null

if(l!=null){//判斷接口非空才能設(shè)置自定義接口回調(diào)

l.onSubmit(updatedData);

}

}

});

bl.show();

}

}實(shí)現(xiàn)MainActivity24publicclassMainActivityextendsAppCompatActivity{

ArrayList<PhoneData>list=newArrayList<>();

ArrayAdapter<PhoneData>adapter;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_main);

ListViewlv=findViewById(R.id.listView);

for(inti=0;i<4;i++){//模擬了8個PhoneData數(shù)據(jù)

list.add(newPhoneData("張三",));

list.add(newPhoneData("李四",));

}

adapter=newArrayAdapter<>(this,

android.R.layout.simple_list_item_1,list);

lv.setAdapter(adapter);

registerForContextMenu(lv);

}

@Override

publicvoidonCreateContextMenu(ContextMenumenu,Viewv,

ContextMenu.ContextMenuInfomenuInfo){

super.onCreateContextMenu(menu,v,menuInfo);

getMenuInflater().inflate(R.menu.ctx_menu,menu);//生成ContextMenu

}

@Override

publicbooleanonContextItemSelected(@NonNullMenuItemitem){

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論