2016-04-19 32 views
0

我有一個程序是什麼使用setContentView來顯示一個佈局,同時我想更新另一個佈局中的文本。setText不在另一個佈局中顯示文本

我沒有找到正在更新的文本,但檢索由getText設置的值會給出更新的值。

我想要使用setText設置的值得到顯示。

public static class Login_fragment extends AppCompatActivity 
{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 

    } 
    public void login_onClick(View view) 
    { 
     Toast.makeText(this,"Login Succesful",Toast.LENGTH_SHORT).show(); 

     startActivity(new Intent(this,MainActivity.class)); 
     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rootview=inflater.inflate(R.layout.nav_header_main,null,false); 
     LinearLayout linearLayout=(LinearLayout)rootview.findViewById(R.id.nav_linear_id); 
     TextView textView=(TextView) rootview.findViewById(R.id.login_name_drawer); 
     String viewq=textView.getText().toString(); 
     Log.v("First",viewq); 
     textView.setText("def"); 
     if(textView.getParent()!=null) { 
      ((ViewGroup) textView.getParent()).removeView(textView); 
     } 
     linearLayout.addView(textView); 
     viewq=textView.getText().toString(); 

     Log.v("Second",viewq); 
    } 

我的XML佈局文件是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="@dimen/nav_header_height" 
android:background="@drawable/side_nav_bar" 
android:gravity="bottom" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:theme="@style/ThemeOverlay.AppCompat.Dark" 
android:id="@+id/nav_linear_id"> 

<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/login_name_drawer" 
android:text="abc"/> 

</LinearLayout> 

我希望文本進行更新,並顯示 「高清」

+0

您正在查看另一種觀點是loginlayout。並且您看不到其他活動佈局。 –

+0

但是你已經開始一個新的活動。您可以通過您的_MainActivity_中的意向傳遞價值 – Piyush

回答

0

1)使用SharedPreference用戶值存儲在LoginActivity。然後在MainActivity中從SharedPreference中檢索值並將該值設置爲textView。

LoginActivity

 SharedPreferences sharedPref = getSharedPreferences("myPrefs", Context. 
MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPref.edit(); 
    editor.putString("user", "test"); 
    editor.commit(); 

在MainActivity的OnCreate()

 SharedPreferences sharedPref = getSharedPreferences("myPrefs", Context. 
MODE_PRIVATE); 
     String user=sharedPref.getString("user",""); 
    TextView textView=(TextView) findViewById(R.id.login_name_drawer); 
    textView.setText(user); 

2.)使用意圖來傳遞用戶值設置在MainActivity TextView的值。

LoginActivity

 Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("user","value"); 
    startActivity(intent); 

在MainActivity的onCreate()

 String user= getIntent().getExtras().getString("user"); 
     TextView textView=(TextView) findViewById(R.id.login_name_drawer); 
     textView.setText(user); 

希望這有助於你

相關問題