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;
    }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容