您好我正在尝试使用google sdk构建应用,我想在其他活动中显示配置文件名称,其中我有EditText。 例如: - GoogleActivity.java到UserInformation.java
我已成功创建谷歌登录,所以现在我想从GoogleActivity.java获取个人资料名称,并希望在UserInformation.java中显示 这是我的USerInformation.java代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_signin);
signInToolbar = (Toolbar) findViewById(R.id.toolbar_signin);
setSupportActionBar(signInToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
signInToolbar.setNavigationIcon(R.drawable.back_arrow);
OtpVerify = (TextView) findViewById(R.id.OtpVerification);
editTextUsername = (EditText) findViewById(R.id.editTextName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
signInToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
getSupportActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>Sign Up</font>"));
OtpVerify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OtpVerification();
}
});
}
private void OtpVerification() {
LayoutInflater inflater = getLayoutInflater();
View OtpDialongScreen = inflater.inflate(R.layout.otpverification, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(OtpDialongScreen);
alert.setCancelable(false);
final AlertDialog dialog = alert.create();
dialog.show();
}
}
任何人都可以帮我在EditText中显示个人资料名称
答案 0 :(得分:0)
您可以使用以下代码从Google API获取用户iformation。或者有关详细信息,请参阅:https://developers.google.com/identity/sign-in/android/people#before_you_begin
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();
现在,用户的名称位于变量personName中。成功登录后,应用程序将不会进入登录页面,因此最好将用户信息保存在 SharedPreference 中。创建类 AppUtils .java
public class AppUtils {
private final static String SHARED_PREFS="AppUtils";
private SharedPreferences prefs;
private SharedPreferences.Editor edit;
public AppUtils(Context context) {
prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
}
public void setUserName(String name) {
edit = prefs.edit();
edit.putString("name", name);
edit.commit();
}
public String getUserName() {
return prefs.getString("name", "");
}
}
因此,在获取用户名后的 GoogleActivity .java类中,通过调用函数setUserName()将其设置为共享首选项对象。
AppUtils utils=new AppUtils(UserInformation.this);
utils.setUserName(personName);
每当你想调用函数getUserName()时。在你的情况下:
AppUtils utils=new AppUtils(UserInformation.this);
editTextUsername = (EditText) findViewById(R.id.editTextName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextUsername.setText(utils.getUserName());