我正在尝试使用 Google Drive SDK 编写应用程序。我想在 PreferenceActivity 中使用 Preference 对象,该对象允许用户选择他们想要上传文件的 Google 帐户。我一直在尝试使用 EditTextPreference 来启动帐户选择器意图。有用。但是,选择帐户后,会出现 EditText 对话框。是否可以禁用对话框?还是我以错误的方式解决这个问题?
首选项.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="@string/title_upload_category">
<EditTextPreference
android:title="@string/title_upload_acct"
android:key="@string/key_upload_acct"/>
</PreferenceCategory>
</PreferenceScreen>
PrefsFragment
public static class PrefsFragment extends PreferenceFragment{
@Override
public void onCreate(Bundle savedinstancestate) {
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(getActivity().getApplicationContext(), Arrays.asList(DriveScopes.DRIVE));
EditTextPreference acctValue = (EditTextPreference)findPreference(getString(R.string.key_upload_acct));
acctValue.setOnPreferenceClickListener(new OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(credential.newChooseAccountIntent(), 1);
return false;
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null){
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
acctValue.setSummary(accountName);
acctValue.setText(accountName);
}
}