`

如何使用QuickContactBadge

阅读更多
参考文章:http://mobile.tutsplus.com/tutorials/android/android-sdk_contact-badge/



If you’ve spent any time on an Android device, you may have noticed how you can click on little Contact images to launch a toolbar with lots of different actions, such as call, text or email that person. In this Quick Tip, you learn how to build this great functionality—called the Quick Contact Badge—into your own applications.
In order to have easy access to contacts, we’ll start with our existing open source code here. We enhance this project, which initially allowed the user to simply choose a contact from a list, and create several different quick contact badges for that contact to illustrate how they work.
Note: This tutorial requires Android 2.0 or higher.
Step 1: Adding an Activity
Start with a new Activity called QuickContactBadgeActivity. Inside the onCreate() method, add a setContentView() method call for a new layout called badge (e.g. R.id.badge).
view plaincopy to clipboardprint?
public class QuickContactBadgeActivity extends Activity { 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
 
        setContentView(R.layout.badge); 
    } 

Step 2: Creating the Layout
Now you need to create a layout using QuickContactBadge controls. The QuickContactBadge control was introduced in Android 2.0 (API Level 5). The following layout creates two QuickContactBadge controls and provides a holder for a third (a FrameLayout control). The QuickContactBadge control is derived from an ImageView control. Thus, you can set the image displayed by the QuickContactBadge control just as you would an ImageView, using the src attribute.
Here’s the final layout we’re using:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <TextView 
        android:text="Sample Quick Contact Badges" 
        android:id="@+id/TextView01" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"></TextView> 
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/pick_contact" 
        android:onClick="onPickContact" 
        android:text="@string/pick_contact_for_badge"></Button> 
    <QuickContactBadge 
        android:id="@+id/badge_small" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/droid_small"></QuickContactBadge> 
    <QuickContactBadge 
        android:id="@+id/badge_medium" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></QuickContactBadge> 
    <FrameLayout 
        android:id="@+id/badge_holder_large" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></FrameLayout> 
</LinearLayout> 
QuickContactBadge controls can launch the contact action bar (as we’re calling it) in three different sizes: small, medium (default), and large. The small action bar contains only the action buttons and minimal details. The medium action bar contains the action buttons and some additional contact info. The large action bar contains lots of actions, contact info and graphics.
Note: The current ADT plug-in for Eclipse allows you to set the window size of the contact action bar in XML. An error is shown when you try to set a value, though. Unfortunately, this means you can’t actually set this attribute in the XML layout file. Instead, you must set the window size programmatically using the setMode() method of the QuickContactBadge class. You will see how in the next step.
Step 2: Configuring the Badges
Within the onCreate() method of the Activity, add the following code, replacing the email address with one in your contacts (add it ahead of time if you need to).
view plaincopy to clipboardprint?
QuickContactBadge badgeSmall = (QuickContactBadge) findViewById(R.id.badge_small); 
badgeSmall.assignContactFromEmail("any@gmail.com", true); 
badgeSmall.setMode(ContactsContract.QuickContact.MODE_SMALL); 
The more information that is associated with the contact tied to the badge, the more action items will be available for use in the contact action bar. For instance, here’s one using my own email address:

And here’s another with a contact that has a web address assigned:

You can use the setExcludeMimeTypes() method of the QuickContactBadge class to remove any actions or information you don’t want to display.
Step 3: Working with Unknown Contacts
The previous example worked well because you already knew your own address or added a contact you knew to exist. What if the contact doesn’t yet exist within your Contacts database? Try it!
Add the following code, this time to look up a phone number that you probably don’t have in your address book:
view plaincopy to clipboardprint?
QuickContactBadge badgeMedium = (QuickContactBadge) findViewById(R.id.badge_medium); 
badgeMedium.assignContactFromPhone("831-555-1212", true); 
badgeMedium.setImageResource(R.drawable.droid_small); 
Note also that this time we are using a medium sized QuickContactBadge. When clicking on the QuickContactBadge for an unknown entry, something interesting happens. The user is asked if they want to add the contact. If they choose yes, they’ll get the option to add the email or phone number to an existing contact or create a new contact. Then, on subsequent presses of this QuickContactBadge, the contact will exist and be found. This can be quite handy.

Step 4: Creating a QuickContactBadge From an Existing Contact
Generally speaking, you don’t know what contacts are on someone’s device. You do, however, have access to the Contacts content provider and can retrieve URIs for each contact as needed. You learned how to launch the contact picker in this previous tutorial.
Here’s an example of how we can use a contact URI to supply the contact information for a QuickContactBadge:
view plaincopy to clipboardprint?
public void onPickContact(View view) { 
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, 
            Contacts.CONTENT_URI); 
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 

 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
        switch (requestCode) { 
        case CONTACT_PICKER_RESULT: 
            Uri contactUri = data.getData(); 
            FrameLayout badgeLargeHolder = (FrameLayout) findViewById(R.id.badge_holder_large); 
 
            QuickContactBadge badgeLarge = new QuickContactBadge(this); 
            badgeLarge.assignContactUri(contactUri); 
            badgeLarge.setMode(ContactsContract.QuickContact.MODE_LARGE); 
            badgeLarge.setImageResource(R.drawable.droid_small); 
            badgeLargeHolder.addView(badgeLarge); 
            break; 
        } 
    } 

Here you use the Contact Uri chosen by the user to configure a QuickContactBadge that the user can click on. In addition, this shows the final, and largest, contact action bar mode.

Using the QuickContactBadge
When might you want to use the QuickContactBadge? Use the QuickContactBadge anywhere that displays friends or lists of contacts, enabling the user to interact with these individuals in other ways. You could also add your email and phone number to the Contacts and provide a QuickContactBadge within your application to give users a quick way to email, call, or message you (or your support team).
Conclusion
In this Quick Tip, you learned how to use the QuickContactBadge control to quickly bring up the contact action bar (available in various sizes) and enable various actions to be taken. The QuickContactBadge is a standard view control available in Android 2.0 and higher, so users should be familiar with its purpose, and therefore appreciate it when developers take advantage of its powerful features. The QuickContactBadge can also save you, the developer, valuable time in creating all of the possible Intent actions that this control provides with ease.

除此之外,还可以看APIDemo-->app->QuickContactsDemo的例子
  • 大小: 47.1 KB
  • 大小: 50.1 KB
  • 大小: 39.8 KB
  • 大小: 58.4 KB
分享到:
评论

相关推荐

    android自定义菜单使用技巧

    `ch06_QuickContactBadge`则可能包含使用QuickContactBadge的实例;`ch06_ucweb_menu`可能是针对特定场景(如UCWeb浏览器)的自定义菜单实现;而`ch06_menu`则可能是基础的菜单资源文件。 总的来说,自定义菜单和...

    通讯录的弹窗效果.

    #### 二、使用QuickContactBadge实现基本通讯录弹窗 1. **布局文件创建**: 在`res/layout`目录下创建布局文件`main.xml`,包含一个`QuickContactBadge`组件。该组件用于显示联系人的图片,并支持点击后弹出一系列...

    Android2.2 API 中文文档系列(8) —— QuickContactBadge

    - **权限需求**:使用 `QuickContactBadge` 需要申请 `android.permission.READ_CONTACTS` 权限,因为该控件涉及访问设备上的联系人信息。 #### 知识点二:QuickContactBadge 的构造与初始化 - **初始化方法**:在...

    Android: 开发短信程序列表界面(QuickContactBadge/ListView混用)

    在这个场景中,`QuickContactBadge` 和 `ListView` 是两个关键组件,它们共同构建了一个高效且用户友好的交互体验。 首先,`QuickContactBadge` 是一个Android SDK提供的视图组件,它主要用于显示联系人头像,并...

    QuickContactDemo

    7. **绑定QuickContactBadge**: 将查询到的联系人ID绑定到QuickContactBadge上,通常使用`setContactUri()`方法。这样在点击QuickContactBadge时,系统会自动弹出对应联系人的QuickContact窗口。 8. **事件监听**: ...

    android 自定义ListView实现动画特效

    此外,还可以使用ValueAnimator或ObjectAnimator实现更复杂的动画效果,如平滑过渡或基于时间的比例变化。 除了动画,自定义ListView还需要注意滚动同步问题。因为ListView在滚动时会复用View,所以我们需要确保在...

    Android 3.0新增UI控件示例说明.rar

    - **使用**:开发者可以通过设置`android:uiOptions="splitActionBarWhenNarrow"`属性或使用`getSupportActionBar()`方法来实现Action Bar。 2. **Fragment** - **定义**:Fragment是Android 3.0引入的一个可重用...

    安卓中文API文档

    本文档涵盖了 Android 2.2 版本的 API,包括 TextView、EditText、AccessibilityService、Manifest、View、ImageView、ImageButton、QuickContactBadge、ZoomButton、CheckBox、RadioButton、Button、ToggleButton、...

    Android 通讯录 字母导航 字母悬浮

    在Android中,这一功能通过`SectionIndexer`接口和`QuickContactBadge`组件共同实现。 `SectionIndexer`接口定义了三个方法:`getPositionForSection(int section)`用于获取指定分段(字母)的第一个索引位置;`...

    安卓Android源码——安卓Android字母排序 类似通讯录字母检索.zip

    4. **QuickContactBadge**:为了增强用户体验,可以在每个联系人项旁边添加QuickContactBadge,当用户点击时可以快速查看或编辑联系人信息。 5. **onSectionChanged()回调**:当用户选择字母栏上的某个字母时,系统...

    android4.0 Contacts 联系人jar包

    在联系人应用中,我们可以使用`ACTION_PICK_CONTACT`或`ACTION_INSERT`等预定义Intent来选择或创建新的联系人。 4. **Permissions**:访问联系人数据通常需要`READ_CONTACTS`和`WRITE_CONTACTS`权限。在Android 4.0...

    android自定义弹出menu

    通过分析和学习这个文件,开发者可以更深入地理解自定义菜单的实现过程,包括布局设计、PopupWindow的使用以及动画的添加等。这个示例不仅适用于创建QuickContactBadge风格的菜单,还可以作为基础,根据需求调整样式...

    01、基本控件1

    10. QuickContactBadge:联系人控件,快速查看或编辑联系人信息。 11. DatePicker/TimePicker:日期和时间选择器,让用户选择日期或时间。 【资源和配置】 Android应用的结构包括界面程序、配置清单文件和资源...

    好的-Android2.2 API中文文档——ImageView.doc

    它有两个已知的直接子类:ImageButton和QuickContactBadge,分别用于显示可点击的图像按钮和快速联系人徽章。除此之外,还有一些间接子类,如ZoomButton,用于处理缩放操作。 ImageView的重要属性包括: 1. `...

    Android 联系人快速索引源码.rar

    5. **QuickContactBadge**:这是一个可以快速查看联系人详细信息的小图标,常用于快速索引栏中,当用户点击特定字母时,显示与该字母相关的联系人。 6. **ListView/RecyclerView**:展示联系人列表的视图组件。...

    android 项目代码

    这个"android项目代码"正是关于这一主题的实践案例,它使用了Qt库来辅助开发,并且具有实用性,适合学习和参考。Qt是一个跨平台的应用程序开发框架,支持包括Android在内的多种操作系统。 首先,我们要理解Android...

    Android简明应用程序开发[原创]

    - **QuickContactBadge**:快速联系组件,常用于显示联系人头像和相关信息。 以上是《Android简明应用程序开发》前五章内容的总结,涵盖了Android应用开发的基础知识和技术要点。希望这些信息能够帮助读者更好地...

    Android_UI_API最全中文文档

    * QuickContactBadge:提供了快速联系人的方式。 * ZoomButton:提供了缩放按钮的方式。 * CheckBox:提供了复选框的方式。 * RadioButton:提供了单选框的方式。 * Button:提供了普通按钮的方式。 * ToggleButton...

Global site tag (gtag.js) - Google Analytics