从不同的Activity设置TextView文本(使用不同的布局)

时间:2017-04-03 19:40:44

标签: java android

现在已经挣扎了一段时间。每当我尝试使用setText()方法从我的MainActivity设置标签的文本时,没有任何反应,我尝试了几种解决方案,它们似乎都不适用于我, 重要的是要知道,我试图改变的布局没有Activity,因为我想在另一个布局中使用它,里面会有一个ListView,无论如何我只想知道我是否有可能从我这样做MainActivity,这是我正在尝试做的一个例子

代码:

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);
}
}

这是我测试过的,即使是评论的内容,我希望当我单击按钮更改为testlayout时,文本显示在TextView中,布局的名称是测试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调用资源,除非你的set layout包含所述元素,我只是希望有一个解决方法。

1 个答案:

答案 0 :(得分: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&amp;的按钮。 Fragment No.2以及名为Fragment
  2. 的初始Fragmentone
  3. 如果按下按钮Fragment No.2FragmentOne将替换为FragmentTwo(其中包含TextViewThis is fragment No.2
  4. 如果按下按钮Fragment No.1FragmentTwo将替换为FragmentOne(其中包含TextViewThis is fragment No.1
  5. <强> 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

    希望这会有所帮助〜