Android面試收集錄 對話方塊、資訊提示和菜單

來源:互聯網
上載者:User

標籤:system   option   int   gen   自訂toast   廣播   remote   組件   cli   

 

1.如何使用AlertDialog顯示一個列表?
  • 使用AlertDialog.Builder.setItems方法。
  • 在setItems中定義DialogInterface.OnClickListener監聽器

 

2.如何使用AlertDialog實現選項按鈕?
  • 定義一個字串數組,用來填充選項
  • new一個AlertDialog.Builder對象
  • setSingleChoiceItems(數組,-1,new OnClickListener{})

 

3.如何使用AlertDialog實現多選?
  • 定義一個字串數組,用來填充選項
  • new一個AlertDialog.Builder對象
  • setMultiChoiceItems(數組,new boolean[]{預設},new DialogInterface.OnMultiChoiceClickListener(){...}) 

 

4.如何修改對話方塊的位置?
  • 使用Window對象擷取對話方塊在視窗位置的對象,Window window=alertDialog.getWindow();
  • 然後使用window.setGravity(Gravity.TOP|Gravity.LEFT)設定位置

 

5.如何改變對話方塊的透明度?
  • 使用Window對象擷取對話方塊視窗位置的對象,Window window=alertDialog.getWindow();
  • 使用window.getAttributes擷取WindowManager.LayoutParams布局參數
  • 然後將布局參數中的alpha設定為浮點數即可
  • 最後調用視窗對象window.setAttributes重新設定改寫的屬性即可

 

6.請寫出顯示一個Toast資訊框的Java代碼?
  • Toast textToast=Toast.makeText(this,"我的資訊",Toast.LENGTH_LONG);
  • textToast.show();

 

7.如何自訂Toast顯示的時間長度?
  • 需要用反射機制來實現
  • 調用Toast.TN.show來顯示,調用Toast.TN.hide來關閉
  • 所以需要先從Toast對象中獲得mTN變數(反射實現)
  • 然後從TN對象總獲得show方法(反射實現)

 

8.請描述一下在狀態列上顯示一個NotificationManager對象的步驟?
  • 先擷取NotificationManager對象,getSystemService(NOTIFICATION_SERVICE)即可得到該對象
  • 直接建立一個Notification,new Notification(R.drawable.icon,"訊息...",System.currentTimeMillis());
  • 建立一個分支機構應用程式的類,PendingIntent對象,PendingIntent.getActivity(this,0,getIntent(),0);保證程式關閉,對象不會釋放
  • 設定通知詳細資料,notification.setLatestEventInfo(this,"天氣轉涼,注意添加衣物",pendingIntent對象);
  • 展示通知,使用通知管理執行個體.notify(R.drawable.icon,notification)

 

9.如何做才能在單擊Notification後彈出一個Activity?
  • 使用PendingIntent.getActivity方法擷取一個PendingIntent對象,這個對象建立了一個分支機構應用程式的類。即時APP已經關閉,對象依舊存在。
  • 調用方式:notification.setLatestEventInfo(this,"訊息標題","訊息內容",上面pendingIntent對象)。
  • 如何發送廣播:PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
  • 如何開始服務:PendingIntent.getService(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);

 

10.如何從狀態列清除Notification?
  • 使用NotificationManager.cancel方法
 11.如何自訂Notification?
  • RemoteViews類來配合,常用於通知和案頭小工具
  • 使用方式:RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.布局名);
  • 通知怎麼關聯:notification.contentView=remoteViews;

 

12.請描述一下如何為一個Activity添加選項菜單?
  • 實現Activity.onCreateOptionsMenu方法
  • 添加菜單即通過menu.add方式添加

 

13.如何自訂選項菜單?
  • 自訂選項菜單用PopupWindow對象來類比選項菜單。
  • 通過重寫onKeyDown來顯示和關閉自訂選項菜單。
 

Android面試收集錄 對話方塊、資訊提示和菜單

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.