zoukankan      html  css  js  c++  java
  • Android面试收集录 对话框、信息提示和菜单

    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来显示和关闭自定义选项菜单。
  • 相关阅读:
    linux sysfs (2)
    微软——助您启动云的力量网络虚拟盛会
    Windows Azure入门教学系列 全面更新啦!
    与Advanced Telemetry创始人兼 CTO, Tom Naylor的访谈
    Windows Azure AppFabric概述
    Windows Azure Extra Small Instances Public Beta版本发布
    DataMarket 一月内容更新
    和Steve, Wade 一起学习如何使用Windows Azure Startup Tasks
    现实世界的Windows Azure:与eCraft的 Nicklas Andersson(CTO),Peter Löfgren(项目经理)以及Jörgen Westerling(CCO)的访谈
    正确使用Windows Azure 中的VM Role
  • 原文地址:https://www.cnblogs.com/Jason-Jan/p/8543633.html
Copyright © 2011-2022 走看看