繁体   English   中英

如何在活动片段中编辑 textview

[英]how to edit textview in fragment from activity

解决了

我想将新文本设置为TextView 我在MainActivity中有新值TextView但此活动不知道此TextView TextView在文件fragment_main.xml中。 片段 class 包含在MainActivity.java文件中,如内部 static class。它是默认生成的活动,片段在 Eclipse。你能帮我吗?

public class MainActivity extends FragmentActivity {

    ... 

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

        ...

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() { } 

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zonemedia.skener.MainActivity"
    tools:ignore="MergeRootFrame" />

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/viewmoje"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    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"
    tools:context="com.zonemedia.skener.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/texturl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_url" />
</LinearLayout>

编辑 1:我试图在 Fragment 中创建公共方法 (=setter) 并从 FragmentActivity 中调用它。 我还尝试直接从片段更改文本: TextView textUrl = (TextView) getView().findViewById(R.id.texturl); textCode.setText("random"); TextView textUrl = (TextView) getView().findViewById(R.id.texturl); textCode.setText("random");

我尝试了@masmic_87 写的。 在片段中我定义TextView fragmentTextView = (TextView)getView().findViewById(R.id.texturl);

然后在活动中我把你的代码。 应用程序仍然崩溃:

java.lang.RuntimeException: Unable to start activity ComponentInfo{.....MainActivity}: java.lang.NullPointerException caused by line 86: ((PlaceholderFragment) fragment).fragmentTextView.setText("something");

最后我尝试直接从 static 片段调用 static 字符串,但 MainActivity.this 带有下划线,原因如下:static 字段 MainActivity.this 应该以 static 方式访问,并且在 scope 中无法访问类型 MainActivity 的封闭实例。

我也尝试将内部 class PlaceholderFragment 移动到 new.java 文件,现在它不是 static。我尝试了这里的所有建议,但没有任何效果

解决了我不知道我的头在哪里。 在片段中我调用了 (TextView) getView() .... 而不是

   View rootView = inflater.inflate(R.layout.fragment_main, container,false);
   mTextView = (TextView) rootView.findViewById(R.id.texturl);
   mTextView.setText("new text");

看看这是否适合您。

在活动中引用您的片段:

PlaceholderFragment fragment = new PlaceholderFragment(); 

然后在片段的textview上定义所需的文本:

((PlaceholderFragment)fragment).fragmentTextView.setText(text you want);

这将是随时将textview的值传递给片段。 如果要在调用片段事务时传递此值,则最好采用以下方式:

Bundle bundle = new Bundle();
bundle.putString("textview", "new_value");
PlaceholderFragment fragment= new PlaceholderFragment();
fragment.setArguments(bundle);
transaction.add(R.id.container, fragment);
transaction.commit();

如果该片段是活动的静态内部类,那么为什么不只将文本存储在活动中的静态字符串中,然后该片段将通过MainActivity.this.stringName直接访问它。

该解决方案将使您的代码耦合度很高,但是如果它是静态内部类,并且他们无论如何都必须像这样传递数据,那么您就不必为此担心。

将其添加到onCreate() 您可以使用绑定到setContentView()

ActivityMainBinding binding = 
ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

textviewname = findViewById(R.id.textviewid);
textviewname.setText("some text");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM