Android面試簡錄——對話框、信息提示和菜單


* 對話框

  • Android的標準對話框最多可以有幾個按鈕,并寫出顯示對話框按鈕的方法。
    標準對話框——AlertDialog類。
    最多有3個按鈕,顯示方法:setButton(),setButton2(),setButton3()。
    AlertDialog.Build提供更直觀的顯示方法:setPositiveButton(),setNeutralButton(),setNegativeButton()。

  • 如何響應Android標準對話框的按鈕單擊事件?
    設置按鈕單擊事件監聽對象:
    AlertDialog.setButton,AlertDialog.setButton2,AlertDialog.setButton3
    AlertDialog.Builder.setPositiveButton,AlertDialog.Builder.setNeUtralButton,AlertDialog.Builder.setNegativeButton.
    舉例:
    new AlertDialog.Builder(this).setTitle("我的對話框").setPositiveButton("關閉", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    //響應按鈕單擊事件
    }
    }).show();

  • 如何將字符串數組作為數據源以列表方式顯示在標準對話框中,并在列表項后面加上選項按鈕?
    AlertDialog.Builder.setSingleChoiceItems.
    實例:
    new AlertDialog.Builder(this).setTitle("我的對話框").setPositiveButton("關閉", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    //響應按鈕單擊事件
    }
    }).show();

    String[] yourname = {"Nancy", "Mike", "Join", "Bob"};
    new AlertDialog.Builder(this).setTitle("choice your name").setSingleChoiceItems(yourname, -1, new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            index = which;
        }
    }).setPositiveButton("OK", new OnClickListener() {
        public void onClick(AlertDialog dialog, int which) {
            new AlertDialog.Builder(Main.this).setMessage("your choice is:" + yourname[index]).show();
        }
    }).show();
    
  • 請描述以下進度條對話框(ProgressDialog)的使用方法。
    android.app.ProgressDialog。
    設置最大進度值:setMax
    設置當前進度值:setProgress
    指定進度增量:incrementProgressBy
    實例:
    ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setIcon(R.drawable.wait);
    progressDialog.setTitle("正在處理數據...");
    progressDialog.setMessage("請稍候...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setMax(100);
    progressDialog.setProgress(20);
    progressDialog.setButton("暫停", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    //處理
    }
    });
    progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    //處理
    }
    });
    progressDialog.show();

  • 如何在標準對話框中任意放置可視組件?
    AlertDialog.Builder.setView.

  • 在Android中顯示對話框有幾種方式?
    1.使用AlertDialog顯示對話框
    2.使用Activity自定義對話框(Theme.Dialog)
    3.使用Activity.showDialog(int)

  • 怎樣改變對話框在屏幕上的顯示位置?
    setGravity.
    實例:
    AlertDialog alertDialog = new AlertDialog.Builder(this).setPositiveButton("確定", null).create();
    alertDialog.setMessage("在頂端顯示對話框");
    Window window = alertDialog.getWindow();
    window.setGravity(Gravity.TOP | Gravity.LEFT);
    alertDialog.show();

  • 使用AlertDialog彈出的對話框無論單機任何按鈕都會關閉對話框。如果想在單擊按鈕后執行一些代碼,并由自己來控制對話框的關閉應如何做呢?
    在AlertDialog中有一個mShowing變量:
    mShowing = true:對話框正在顯示,系統會關閉對話框。
    mShowing = false:系統認為對話框已關閉。
    try {
    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
    field.setAccessible(true);
    field.set(dialog, false);
    } catch (Exception e) {

    }
    
  • 如何改變對話框的透明度?
    Window.alpha.
    AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("半透明對話框").setPositiveButton("確定", null).create();
    Window window = alertDialog.getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.alpha = 0.7f;
    window.setAttributes(lp);
    alertDialog.show();


* 信息提示

Android SDK提供了兩種用于顯示信息的方式:Toast 和 Notification.

Toast提示框

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

  • 判斷下列寫法是否正確?
    正確:
    Toast textToast = Toast.makeText(this, "我的信息", Toast.LENGTH_LONG); //這里的makeText會創建一個帶TextView的Toast對象
    textToast.setText("誰的信息");
    textToast.show();
    錯誤:
    Toast toast = new Toast(this); //這里的new會創建一個Toast對象
    toast.setText("我的信息");
    textToast.show();
    正確:
    View view = getLayoutInflater().inflate(R.layout.dialog, null);
    Toast toast = new Toast(this);
    toast.setView(view); //在創建的Toast對象里加入自定義的View對象
    toast.show();

  • 怎么控制Toast信息框的顯示和關閉?
    在系統內部會使用一個隊列來管理多個Toast。
    Toast.show():只是把Toast加入到隊列中。
    控制顯示和關閉:不能把Toast放入隊列中,需要直接調用顯示和關閉的方法。
    Toast中有個內嵌類TN:show/hide.
    通過反射獲取TN對象,調用show/hide。
    Toast toast = Toast.makeText(this, "永不關閉的Toast", Toast.LENGTH_LONG);
    try {
    Field field = toast.getClass().getDeclaredField("mTN");
    field.setAccessible(true);
    Object obj = field.get(toast);
    Method method = obj.getClass().getDeclaredMethod("show", null);
    method.invoke(obj, null);
    } catch (Exception e) {

    }
    
  • 如何顯示一個不可獲得焦點,也不影響用戶進行其他操作的窗口?
    1.Toast
    2.PopupWindow:
    View layout = getLayoutInflater().inflate(R.layout.toast, null);
    PopupWindow popupWindow = new PopupWindow(layout, 200, 100);
    popupWindow.setTouchable(false);
    popupWindow.showAtLocation(layout, Gravity.CENTER_HORIZONTAL, 20, 0);
    關閉:popupWindow.dismiss();


通知:Notification

  • 請描述一下在狀態欄上顯示Notification的實現步驟。
    1.getSystemService -> NotificationManager
    2.創建一個Notification對象
    3.創建PendingIntent對象
    4.Notification對象.setLastestEventInfo -> 設置Notification的詳細信息。
    5.NotificationManager.notify(ID) -> 顯示Notification信息
    實例:
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon, "您有新消息了", System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
    notification.setLatestEventInfo(this, "天氣預報", "晴轉多云", contentIntent);
    notificationManager.notify(R.drawable.icon, notification);
  • 怎樣單擊Notification后彈出一個Activity?
    1.使用PendingIntent.getActivity -> PendingIntent對象
    2.setLastestEventInfo(...,PendingIntent對象)

【拓展】單擊Notification后觸發的動作:
Activity/BroadcastReceiver/Service。
發送廣播:
Intent intent = new Intent("MYBROADCAST");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
開始服務:
Intent intent = new Intent(this, MyService.class);
PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

  • 如何從狀態欄中清除Notification?
    NotificationManager.cancel(ID).
    NotificationManager.canelAll().
  • 如何將Notification放在“正在進行的”欄中?(永久存在,除非用cancel方法清除)
    設置Notification的flags屬性:
    Notification notification = new Notification(R.drawable.smile, "收到短信了", System.currentTimeMillis());
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, Main.class), 0);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(this, "短信內容", "最近在忙什么", pendingIntent);
    notificationManager.notify(R.drawablw.smile, notification);
  • 如何自定義Notification,并說出Notification支持哪些可視組件?
    設置Notification.contentView變量。
    如下:
    Notification notification = new Notification(R.drawable.smile, "自定義Notification", System.currentTimeMillis());
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, Main.class), 0);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
    remoteViews.contentView = remoteViews;
    remoteViews.contentIntent = pendingIntent;
    支持的可視組件:
    布局:FrameLayout/LinearLayout/RelativeLayout
    組件:AnalogClock/Button/Chronometer/ImageButton/ImageView/ProgressBar/TextView

* 菜單

  • Android支持哪幾種菜單?
    選項菜單,上下文菜單,子菜單。
  • 哪些Android支持的菜單中菜單項可以顯示圖像?
    選項菜單。
    其他兩種的菜單頭可以顯示圖像,但是菜單項不能顯示。
  • 如何為一個Activity添加選項菜單?
    實現Activity.onCreateOptionsMenu方法,并利用Menu對象添加菜單。
    public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(1, 1, 1, "菜單項1");
    menu.add(1, 2, 2, "菜單項2");
    menu.add(1, 3, 3, "菜單項3");
    return true;
    }

【拓展】registerContextMenu -> 將上下文菜單與可視組件綁定
Button button = (Button) findViewById(R.id.button);
registerForContextMenu(button);

  • 怎么用菜單項顯示Activity?
    MenuItem menuItem = menu.add(1, 1, 1, "菜單項");
    addMenuItem.setIntent(new Intent(this, MyActivity.class));
  • 響應菜單項單擊事件有哪些方法?
    1.onMenuItemClick
    2.onOptionsItemSelected
    3.onMenuItemSelected

【拓展】多個菜單項事件如何響應?
1.當onMenuItemClick方法返回true時,另兩種方法都失效了。
2.未設置onMenuItemClick時:根據在onMenuItemSelected方法中調用父類的onMenuItemSelected方法的位置決定先調用哪個方法。
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
//在super.onMenuItemSelected(featureId, item)中調用了onOptionsItemSelected方法
super.onMenuItemSelected(featureId, item);
Log.d("onMenuItemSelected:ItemId=", String.valueOf(item.getItemId()));
return true;
}

  • 如何自定義選項菜單?
    1.顯示和關閉自定義選項菜單:在onKeyDown方法中截獲按"menu","back"鍵的動作。
    2.自定義菜單實現:PopupWindow對象模擬選項菜單。
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch(keyCode) {
    case KeyEvent.KEYCODE_MENU:
    if (state == 1)
    return false;
    layout = getLayoutInflater().inflate(R.layout.menu_layout, null);
    pop = new PopupWindow(layout,
    getWindowManager().getDefaultDisplay().getWidth(),
    getWindowManager().getDefaultDisplay().getHeight());
    pop.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
    state = 1;
    return false;
    case KeyEvent.KEYCODE_BACK:
    if (state == 1) {
    pop.dismiss();
    state = 2;
    } else if (state == 2) {
    finish();
    }
    return false;
    }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容