1.android ͨѶ¼ Դ?讯录?
android ͨѶ¼ Դ??
1. Modify the `getTelClick()` method:
- Change the method name from `getTelClick` to `onGetTelClick` for better readability and adherence to naming conventions.
- Ensure that the method signature is correct and that it is properly calling the new method.
2. Modify the Toast breadcrumb提示:
- Remove the unnecessary string concatenation within the Toast message.
- Use string resources for the Toast message to improve localization support.
3. Modify the method for获取联系人名字:
- Correct the column index for the contact's name to match the correct field from the ContactsContract.
4. Adjust the location slightly:
- Ensure that the code for finding the contact's phone number is inside the correct `if` block to avoid unnecessary queries.
Here is the modified section of the code:
```java
// ... (previous code)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_name);
et = (EditText) this.findViewById(R.id.mobile);
}
public void onGetTelClick(View view) {
String name = et.getText().toString().trim();
getContactNumber(name);
}
/
*** 通过输入的姓名获取电话号码
*/
public void getContactNumber(String name) {
// 使用ContentResolver查找联系人数据
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// 遍历查询结果,找到所需号码
while (cursor.moveToNext()) {
// 获取联系人ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// 获取联系人的源码天天影视2.2源码名字
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (name.equals(contactName)) {
// 使用ContentResolver查找联系人的电话号码
Cursor phone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{ contactId}, null);
if (phone.moveToNext()) {
String phoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, phoneNumber, Toast.LENGTH_SHORT).show(); // Use string resource for the message
Log.d(TAG, "电话:" + phoneNumber);
break;
}
}
}
}
```
Ensure that you have defined a string resource for the Toast message in your `res/values/strings.xml` file, like this:
```xml
电话号码:%1$s
```
And use it in the Toast.makeText method as shown above.
2024-11-20 19:26941人浏览
2024-11-20 18:59995人浏览
2024-11-20 18:162758人浏览
2024-11-20 17:44135人浏览
2024-11-20 17:41488人浏览
2024-11-20 17:191073人浏览
1.ACPACP格式文件2.内容管理系统有哪些3.alfresco cms 是什么,哪里有关于它的中文资料?4.开源系统开源系统风险ACPACP格式文件 在EditPlus这款文本编辑器中,存在一
今1)天立院龍頭戰落幕,將由國民黨的「韓江配」帶領新國會,兩人宣誓就職後,韓國瑜期盼,2024年的立法院會是個講理的立法院,不再像是叢林,充滿暴力陽剛之氣,也不是「講拳頭的地方」。秘書長人選? 韓國瑜
立法院龍頭確定「韓江配」,但幕後爆出的白綠「珊昌配」爭議不斷延燒,民進黨稱柯文哲「主動」打電話給民進黨醫界大老,希望民進黨支持黃珊珊當立法院長,副院長則無條件接受民進黨推出的人選,但柯文哲說是民進黨先