我需要在用户输入联系人号码时显示联系人姓名。然后单击显示联系人姓名按钮。 我的布局Xml文件内容3字段 1.编辑文本(用户输入联系号码) 2.Text View(按下按钮时显示联系人姓名的位置) 3.Button 这是我的活动课,以便更好地理解
public class ContactNameActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button buttonshow = (Button) findViewById(R.id.button_ShowName);
final EditText textPhoneNo = (EditText) findViewById(R.id.Edittext_Number);
final TextView tv=(TextView)findViewById(R.id.tv_ContactName);
buttonshow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//here i need String ContactName
tv.setText(String.valueOf(ContactName));
}
});
}
}
答案 0 :(得分:-1)
private String getContactNameFromNumber(String number) {
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor cursor = context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);
if (cursor.moveToFirst())
{
name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
return name;
}