ImageView及其子类(三)

时间:2023-03-09 13:28:28
ImageView及其子类(三)

实例:使用QuickContactBadge关联联系人

     QuickContactBadge继承了ImageView,因此它的本质也是图片,也可以通过android:src属性指定他显示的图片。QuickContactBadge额外增加的功能是;该图片可以关联到手机中指定的联系人,当用户单击该图片时,系统将会打开相应的联系人的联系方式界面。

为了让QuickContactBadge与特定联系人关联,可以调用如下方法进行关联。

  • assugnContactFromEmail(String emailAddress,boolean lazyLookup):将该图片关联到指定E-mail地址对应的联系人。
  • assignContactFromPhone(String phoneNumber,boolean lazyLookup):将该图片关联到指定电话号码对应的联系人。
  • assignContactUri(Uri contactUri):将该图片关联到特定Uri对应的联系人。

该实例的界面布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<QuickContactBadge android:id="@+id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:text="我的偶像" />
</LinearLayout>

上面的布局文件非常简单,它只是包含了一个QuickContactBadge组件与TextView组件。接下来在Activity代码中可以让QuickContactBadge与特定联系人建立关联。下面是该Activity的代码。

package org.crazyit.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*; public class QuickContactBadgeTest extends Activity { QuickContactBadge badge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quick_contact_badge_test);
//获取QuickContactBadge组件
badge=(QuickContactBadge)findViewById(R.id.badge);
//将QucikContactBadge组件与特定电话号码对应的联系人建立连接
badge.assignContactFromPhone("13521857652", false);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quick_contact_badge_test, menu);
return true;
} }

运行该实例将看到如下效果:

ImageView及其子类(三)