这是一个让我发疯的问题。
我需要两个稍微不同(可能是参数化的)和非常具体的偏好类型。
一个是EditText,旁边有一个Button。 EditText允许您输入(修改)电话号码,同时按钮打开“联系人”对话框,您可以选择联系人号码。
其他偏好非常相似,如果不相同,但允许您从另一个联系人中选择一个电子邮件(并不是说电话和电子邮件来自同一个联系人,他们可以 - 通常应该 - 来自不同的人)。
这些自定义首选项应该在PreferenceActivities和PreferenceFragments中使用。
更好的是,如果EditText也可以在左侧显示联系人照片 - 但这只是在做梦,我知道......
你知道我怎么能做到这样的事情呢?
我甚至无法弄清楚这样做的最佳方法是使用PreferenceScreen中的意图还是扩展DialogPreference或......谁知道什么。
现在,我的英语可能有问题,我最好通过图像的帮助解释我需要的东西(这是一系列动作):
提前谢谢!
答案 0 :(得分:2)
嗯,事实证明,DialogPreferences无法覆盖onActivityResult,这是至关重要,用于在您从联系人列表中选择联系人后获得结果。
所以我不得不辞职并找到其他解决办法 一个简单的EditTextPreference ......对我的口味来说太简单了,我扩展了一个RingTonePreference并根据我的需要进行了调整。
您将需要:一个类(首选项本身),一个可设置样式的属性(只是用于设置用于选择电话号码或电子邮件地址的枚举)和首选项定义文件。
我们走了:
CLS_Prefs_Contact.java
/**
* CLS_Prefs_Contact class
*
* This is the class that allows for a custom Contact Picker Preference
* (auto refresh summary).
*
* @category Custom Preference
* @author Paranoid Eyes
* @copyright Paranoid Eyes
* @version 1.0
*/
package com.paranoideyes.contactpreference_rt;
/* ---------------------------------- Imports ------------------------------- */
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.provider.ContactsContract;
import android.util.AttributeSet;
/**
* @attr ref android.R.styleable#ContactPreference_contactInfo
*/
public class ContactPreference extends RingtonePreference
{
/* ----------------------------- Constants ------------------------------ */
/*
private static final String nsContact =
"http://schemas.android.com/apk/res/com.lucacrisi.contactpreference_rt";
*/
private static final String nsContact =
"http://schemas.android.com/apk/res-auto";
/* ----------------------------- Variables ------------------------------ */
//private static Contact_Info contactInfo = null;
private int contactInfo = 0;
private final String defaultSummary = "---";
/* ------------------------------ Objects ------------------------------- */
private static Context ctx = null;
private static SharedPreferences prefs = null;
/* ---------------------------- Constructors ---------------------------- */
public ContactPreference(final Context context, final AttributeSet attrs)
{
super(context, attrs);
ctx = context;
// Read attributes
contactInfo = attrs.getAttributeIntValue(nsContact, "contactInfo", 0);
//System.out.println("Constructor - " + contactInfo);
prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
summary_Update();
}
/* ----------------------------- Overrides ------------------------------ */
@Override
public final boolean onActivityResult
(final int requestCode, final int resultCode, final Intent data)
{
boolean result = false;
if (super.onActivityResult(requestCode, resultCode, data))
{
if (data != null)
{
//
getContactInfo(data);
//
final Uri uri = data.getData();
if (callChangeListener(uri != null ? uri.toString() : ""))
{
result = true;
}
}
}
return result;
}
@Override
protected final void onPrepareRingtonePickerIntent(final Intent tnt)
{
tnt.setAction(Intent.ACTION_PICK);
tnt.setData(ContactsContract.Contacts.CONTENT_URI);
}
/* ------------------------------ Methods ------------------------------- */
private final void getContactInfo(final Intent data)
{
//final String noData = getString(R.string.missing_data);
//String result = noData;
String result = "";
final Cursor cur =
ctx.getContentResolver().query(data.getData(), null, null, null, null);
while (cur.moveToNext())
{
final String contactId =
cur.getString
(
cur.getColumnIndex
(
ContactsContract.Contacts._ID
)
);
//System.out.println("getContactInfo - " + contactInfo);
switch(contactInfo)
{
case 0: // phone
{
// Find the phone numbers
String hasPhone = cur.getString
(
cur.getColumnIndex
(
ContactsContract.Contacts.HAS_PHONE_NUMBER
)
);
if (hasPhone.equalsIgnoreCase("1"))
{
hasPhone = "true";
}
else
{
hasPhone = "false" ;
}
if (Boolean.parseBoolean(hasPhone))
{
final Cursor phones = ctx.getContentResolver().query
(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +
" = " + contactId, null, null
);
String number = ""; //noData;
if (phones.getCount() > 0)
{
phones.moveToFirst();
number = phones.getString
(
phones.getColumnIndex
(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
);
}
phones.close();
result = number;
//result = ((number == "") ? noData : number);
}
break;
}
case 1: // email
{
// Find the email addresses
final Cursor emails = ctx.getContentResolver().query
(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID +
" = " + contactId, null, null
);
String email = ""; //noData;
if (emails.getCount() > 0)
{
emails.moveToFirst();
email = emails.getString
(
emails.getColumnIndex
(
ContactsContract.CommonDataKinds.Email.DATA
)
);
}
emails.close();
result = email;
//result = ((email == "") ? noData : email);
break;
}
}
}
cur.close();
// Here do the magic and put the value into the Preferences
setting_Write(getKey(), result);
summary_Update();
}
private final static void setting_Write(final String key, final String value)
{
// Write the value
prefs.edit().putString(key, value).commit();
}
private final void summary_Update()
{
// Read the value and set the summary
String str = prefs.getString(getKey(), "");
if ("".equals(str))
{
str = defaultSummary;
}
setSummary(str);
}
}
RES /值/ attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CLS_Prefs_Contact">
<attr name="contactInfo">
<enum name="phone" value="0" />
<enum name="email" value="1" />
</attr>
</declare-styleable>
</resources>
RES / XML /的prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:contact="http://schemas.android.com/apk/res-auto"
>
<com.paranoideyes.contactpreference_rt.CLS_Prefs_Contact
android:key="phone_nr"
android:title="@string/phone_nr"
contact:contactInfo="phone"
/>
<com.paranoideyes.contactpreference_rt.CLS_Prefs_Contact
android:key="email_ad"
android:title="@string/email_ad"
contact:contactInfo="email"
/>
</PreferenceScreen>
RES /值/ strings.xml中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ContactPreference Sample</string>
<string name="phone_nr">Choose a phone number</string>
<string name="summary_pn">Not set</string>
<string name="email_ad">Choose an email</string>
<string name="summary_ea">Not set</string>
</resources>
所以,这就是我最终的结果: