美文网首页安卓Android面试题Android面试宝典面了个试
Android面试简录——对话框、信息提示和菜单

Android面试简录——对话框、信息提示和菜单

作者: nancymi | 来源:发表于2015-04-07 19:42 被阅读584次

* 对话框

  • 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;
    }
    }

相关文章

网友评论

    本文标题:Android面试简录——对话框、信息提示和菜单

    本文链接:https://www.haomeiwen.com/subject/rjezxttx.html