2017-04-03 77 views
0

现在一直在努力解决这个问题。每当我尝试使用setText()方法设置我的MainActivity中的标签文本时,什么也没有发生,我尝试了几种解决方案,但它们都不适合我, 尽管知道这一点很重要,即布局Im试图改变没有活动,因为我想在另一个布局里面使用它,里面会有一个ListView,无论如何我只是想知道是否可以从我的MainActivity中做到这一点,这里是我的一个例子尝试做从不同的活动中设置TextView文本(使用不同的布局)

代码:

public class MainActivity extends AppCompatActivity { 

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

public void letsee(View view) { 

//LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    View content = (View)getLayoutInflater().inflate(R.layout.test, null); 

    //View vi = inflater.inflate(R.layout.test, null, false); 

    TextView tv = (TextView)content.findViewById(R.id.testview); 

    tv.setText("DOES THIS EVEN WORK?"); 

    setContentView(R.layout.test); 
} 
} 

这是我测试过,就算有什么评论,我想显示在TextView的文本,当我点击按钮切换到testlayout,布局的名字的测试BTW

XML:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/rtest"> 


    <TextView 
    android:id="@+id/testview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 
    </RelativeLayout> 

我知道,除非你的组布局包括所说的元素,您无法通过其ID调用资源,我只希望有一个解决方法是。

+0

您可以使用片段吗,将片段绑定到活动并将视图附加到该片段?还是必须来自活动? –

+0

你需要先拨打setContentView –

+0

@paul_hundal抱歉,我不太确定一个片段是什么 – SoulBlack

回答

1

您正在使用TextView而未将test布局设置为内容视图。尝试先设置内容视图setContentView(R.layout.test)然后根据您的需要使用视图。

试试这个:

public class MainActivity extends AppCompatActivity { 

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

     Button button = (Button) findViewById(R.id.YOUR_BUTTON); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       letsee(); 
      } 
     }); 
    } 

    public void letsee() { 
     setContentView(R.layout.test); 

     TextView tv = (TextView) findViewById(R.id.testview); 
     tv.setText("DOES THIS EVEN WORK?"); 
    } 
} 

更新:

您可以使用Fragment改变你的看法。下面是一个完整的例子:

  1. 在我们的MainActivity中,有一个名为Fragment No.1 & Fragment No.2两个按钮和一个初始Fragment名为Fragmentone
  2. 如果按钮Fragment No.2按下,FragmentOne将由FragmentTwo(其中包含TextView值代替This is fragment No.2
  3. 如果按下按钮Fragment No.1,则FragmentTwo将被替换为FragmentOne(其包含TextViewThis is fragment No.1

activity_main.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="match_parent" 
    android:orientation="vertical" > 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Fragment No.1" 
     android:onClick="selectFrag" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:onClick="selectFrag" 
     android:text="Fragment No.2" /> 

    <fragment 
     android:name="com.android.fragmentstest.FragmentOne" 
     android:id="@+id/fragment_place" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

fragment_one.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="match_parent" 
    android:orientation="vertical" 
    android:background="#00ffff"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="This is fragment No.1" 
      android:textStyle="bold" /> 

</LinearLayout> 

fragment_two.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="match_parent" 
    android:orientation="vertical" 
    android:background="#ffff00"> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="This is fragment No.2" 
      android:textStyle="bold" /> 

</LinearLayout> 

FragmentOne。java的

package com.android.fragmentstest; 

import android.app.Fragment; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class FragmentOne extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 

     //Inflate the layout for this fragment 

     return inflater.inflate(
       R.layout.fragment_one, container, false); 
    } 
} 

FragmentTwo.java

package com.android.fragmentstest; 

import android.app.Fragment; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class FragmentTwo extends Fragment{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 

     // Inflate the layout for this fragment 

     return inflater.inflate(
       R.layout.fragment_two, container, false); 
    } 
} 

MainActivity.java

package com.android.fragmentstest; 

import android.os.Bundle; 
import android.view.View; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 


public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
    } 

    public void selectFrag(View view) { 
     Fragment fr; 

     if(view == findViewById(R.id.button2)) { 
      fr = new FragmentTwo(); 

     }else { 
      fr = new FragmentOne(); 
     } 

     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_place, fr); 
     fragmentTransaction.commit(); 

    } 
} 

你也可以看到这个tutorial

希望这会有所帮助〜

+0

这是行不通的,它不是我要找的东西,但它起作用。自从您首先设置contentview以来,有点看到了它。非常感谢。 – SoulBlack

+0

@SoulBlack查看我的更新回答。我想这正是你想要达到的。 – FAT

+0

在检查了更新后的答案后,我一直在思考我可以用这个新的片段概念应用的所有可能的事情(对我来说至少是新的),无论如何感谢帮助的人,这是正确的答案 – SoulBlack

相关问题