android - 如何访问导航抽屉标题项目? [英] android - How to access the Navigation Drawer Header Items?

查看:30
本文介绍了android - 如何访问导航抽屉标题项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 GoogleSignIn 按钮登录并获取用户名以将其显示在导航栏标题的 TextView 中,但我无法访问它.

i need to sign in with GoogleSignIn button and get the name of the user to show it into the TextView in the header of the Navigation Bar, but i cant access it.

MainActivity.java

 public class MainActivity extends AppCompatActivity
 implements NavigationView.OnNavigationItemSelectedListener, 
 GoogleApiClient.OnConnectionFailedListener {

ImageView photoImageViewe;
TextView nameTextView, emailTextView, txt3ady;

private GoogleApiClient googleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Drawer Inflater
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    nameTextView = (TextView) findViewById(R.id.nameTextView);
    emailTextView = (TextView) findViewById(R.id.emailTextView);
    photoImageViewe = (ImageView) findViewById(R.id.photoImageView);

    //Google SignIn
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    googleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this,this)
            .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
            .build();
}//end onCreate

public void  logOut(View view){
    Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            if (status.isSuccess())
            {
                goLogInScreen();
            }
            else {
                Toast.makeText(getApplicationContext(), "not_signed_out", Toast.LENGTH_SHORT).show();
            }
        }
    });
}//end logOut()

public void revoke(View view) {

    Auth.GoogleSignInApi.revokeAccess(googleApiClient).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            if (status.isSuccess()){
                goLogInScreen();
            }
            else{
                Toast.makeText(getApplicationContext(),"not_revoke",Toast.LENGTH_SHORT).show();
            }
        }
    });

}//end revoke()

@Override
protected void onStart() {
    super.onStart();

    OptionalPendingResult<GoogleSignInResult> op = Auth.GoogleSignInApi.silentSignIn(googleApiClient);
    if (op.isDone())
    {
        GoogleSignInResult result = op.get();
        handleSignInResults(result);
    }
    else
    {
        op.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
                handleSignInResults(googleSignInResult);
            }
        });
    }//end else
}//end onStart()

private void handleSignInResults(GoogleSignInResult result) {

    if (result.isSuccess())
    {
  //////HERE//////////////////////////////////////
        GoogleSignInAccount account = result.getSignInAccount();
        assert account != null;
        nameTextView.setText(account.getDisplayName());
        //emailTextView.setText(account.getEmail());

        //Glide.with(this).load(account.getPhotoUrl()).into(photoImageViewe);
    }//end if
    else {
        goLogInScreen();
    }//end else

}//end handleSignInResults()

private void goLogInScreen() {

    Intent intent = new Intent(this,LogInActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
 Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}//end goLogInScreen

//END GOOGLE SIGNIN

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}//end onBackPressed

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
 }

 @Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Toast.makeText(getApplicationContext(),"no internet", 
 Toast.LENGTH_LONG).show();
   }
  }//end class Main Activity

注意: login_activity 没有任何问题并且可以正常工作,问题是我如何从 Google Button 获取这些信息并将其放在导航抽屉标题菜单的 TextView 上(只是名称)我会做其他的(电子邮件照片)

Notice: the login_activity doesn't have any problem and it works, the problem is how i get these info from Google Button and put it on the navigation drawer header menu's TextView(just the name) and i will do the others (email-photo)

推荐答案

您需要扩充标题视图,因为它不会自动扩充.

You need to inflate the header view as it is not inflated automatically .

View header = mNavigationView.getHeaderView(0);
mNameTextView = (TextView) header.findViewById(R.id.nameTextView);
mNameTextView.setText("XYZ");

这篇关于android - 如何访问导航抽屉标题项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆