Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在导航抽屉的标题中显示用户的登录信息?_Java_Android_Navigation Drawer - Fatal编程技术网

Java 如何在导航抽屉的标题中显示用户的登录信息?

Java 如何在导航抽屉的标题中显示用户的登录信息?,java,android,navigation-drawer,Java,Android,Navigation Drawer,我正在开发一个应用程序,在该应用程序中,用户将在登录后通过提供用户名和电子邮件登录。一个页面将打开导航抽屉,然后我想在导航抽屉的标题中显示用户名和电子邮件。我尝试过很多方法,比如从edittext获取文本并将其显示到textview中,但没有任何效果,我不知道如何实现这一点 下面是文件 Navigation activity: package com.example.hp.another; import android.support.annotation.NonNull; import a

我正在开发一个应用程序,在该应用程序中,用户将在登录后通过提供用户名和电子邮件登录。一个页面将打开导航抽屉,然后我想在导航抽屉的标题中显示用户名和电子邮件。我尝试过很多方法,比如从edittext获取文本并将其显示到textview中,但没有任何效果,我不知道如何实现这一点

下面是文件

Navigation activity:
package com.example.hp.another;


import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


    private DrawerLayout mdrawerlayout;
    private ActionBarDrawerToggle mtoggie;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        mdrawerlayout=(DrawerLayout) findViewById(R.id.drawer);
        mtoggie= new ActionBarDrawerToggle(this, mdrawerlayout, R.string.Open, R.string.Close);
        mdrawerlayout.addDrawerListener(mtoggie);
        mtoggie.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mtoggie.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        return false;
    }
}
header.xml
loginactivity.xml

首先使用以下代码访问标题文本视图

        View nav = navigationView.getHeaderView(0);
        TextView Username= (ImageView)nav.findViewById(R.id.Username);
        TextView Email= (ImageView)nav.findViewById(R.id.Email);
登录后,在SharedReferences中保存用户名和电子邮件,并将值设置为这些文本视图

每当我们在SharedReference中存储或检索数据时,我们都需要提交特定的用途

private SharedPreferences sharedPref;
private static String PREF_STRING = "pref_value";

  // method to store value in sharedPreferences
 public void login(String username_params, String email_params)
 {
   // initialize the sharePreferences
   sharedPref = getApplicationContext().getSharedPreferences(PREF_STRING,0);
   // use the editor to save the data
   SharedPreferences.Editor editor = sharedPref.edit();
   // add data to sharedPreferences using put() method

   editor.putString("username",username_params);
   editor.putString("email",email_params);

   // only this won't save your data to preferences, we need to commit this 
   transaction
   editor.commit(); // only after commit() data is saved in sharedPreferences
 }
现在,在将数据保存到首选项中后,我们可以从应用程序的任何部分访问该值

  public void getDataFromPrefernces()
  {
      // initialize the sharePreferences
      SharedPreferences prefefnces = 
      getApplicationContext().getSharedPreferences(PREF_STRING,0);
      // use the editor to retrieve the data
      SharedPreferences.Editor editor = prefefnces.edit();

      // here we save the sharedpreference values to string variables
      String username = prefefnces.getString("username",""); // make sure 
      you donot mismatch the name of the key/ it must be same with that you 
      stored
      String email = prefefnces.getString("email",""); 

      // again to get data we need to commit()
      editor.commit();

      // you can use these values from string variables wherever you need
   }

我在导航活动中进行了此更改。它不起作用SharedReferences sp=GetSharedReferences(“键”,0);SharedReferences.Editor SharedEdit=sp.edit();putString(“用户名”,UserName.getText().toString());shareditr.putString(“Email”,Email.getText().toString());SharedReferences SharedReferences=GetSharedReferences(“键”,0);字符串username=SharedReferences.getString(“用户名”,“用户名”);String useremail=sharedPreferences.getString(“email”,”);您尚未输入首选项值SharedReferences sp=GetSharedReferences(“键”,0);SharedReferences.Editor SharedEdit=sp.edit();putString(“用户名”,UserName.getText().toString());shareditr.putString(“Email”,Email.getText().toString());提交();SharedReferences SharedReferences=GetSharedReferences(“键”,0);SharedReferences.Editor Editor=SharedReferences.edit();字符串username=SharedReferences.getString(“username”,”);String useremail=sharedPreferences.getString(“email”,”);editor.commit();我认为我没有正确使用共享偏好方法,因为我不知道如何使用它。