【问题标题】:Android How to change fragment TextView using java in MainActivivityAndroid 如何在 MainActivivity 中使用 java 更改片段 TextView
【发布时间】:2016-04-26 22:52:35
【问题描述】:

我使用java 在activity_main.xml 中插入一个片段,我将片段以线性布局放置在activity_main 中。 片段 xml 文件有一个 textView。

当应用程序使用 java 从 MainActivity 启动时、应用程序启动时以及调用 onCreate 时,我如何更改 TextView? 我尝试了一些解决方案形式 stacoverflow 但我可以让它工作。 当他开始加载应用程序时,模拟器会崩溃。

这里是activity_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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.example.android.fragment_promjenaviewprekojave.MainActivity">


    <LinearLayout
        android:id="@+id/linearLayoutFragment_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    </LinearLayout>
</LinearLayout>

这里是片段代码:

package com.example.android.fragment_promjenaviewprekojave;

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

public class BlankFragment extends Fragment {
    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }
}

这是只包含 1 个 TextView 的片段的 xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.fragment_promjenaviewprekojave.BlankFragment">

<TextView
    android:id="@+id/fragmentTextView_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

这是我要更改片段TextView的MainActivity.java

package com.example.android.fragment_promjenaviewprekojave;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        BlankFragment objA = new BlankFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.linearLayoutFragment_id, objA);

        fragmentTransaction.commit();

        TextView textView = (TextView) findViewById(R.id.fragmentTextView_id);
        textView.setText("We add text to fragment TextView");
    }
}

【问题讨论】:

    标签: java android fragment


    【解决方案1】:

    在您的 BlankFragment 代码中,更改

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }
    

    到:

    //Declare a member to hold a reference to your TextView
    private TextView  myTextView;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_blank, container, false);
        myTextView = (TextView) v.findViewById(R.id.fragmentTextView_id);
        myTextView .setText("Whatever text you want");
        return (v);
    }
    
    //Also declare a function to modify the text on your fragment
    public void setCustomText(String str) {
        if (myTextView != null) {
          myTextView.setText(str);
        }
    }
    

    现在回到你的活动中,你可以调用

    objA.setCustomText("Hello World!");
    

    请记住,您需要在活动中存储对 objA 的引用,例如

    private BlankFragment  objA;
    

    onCreate 将是:

    objA = new BlankFragment();
    

    最后,一旦视图全部就位并绘制好,此方法就可以正常工作。 如果您希望在创建时更改片段内的文本,您需要在片段上使用newInstance 模式,您可以在其中将文本作为参数传递。

    类似这样的:

    objA = BlankFragment.newInstance("Hello world");
    

    newInstance 在您的片段中定义如下:

    public static BlankFragment newInstance(String str) {
        BlankFragment f = new BlankFragment();
        Bundle args = new Bundle();
        args.putString("TextTag", str);
        f.setArguments(args);
        return (f);
    }
    

    然后您可以像这样在 Fragment 的onCreate 中检索此值:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_blank, container, false);
        myTextView = (TextView) v.findViewById(R.id.fragmentTextView_id);
        String text = getArguments().getString("TextTag", "");
        myTextView .setText(text);
        return (v);
    }
    

    【讨论】:

    • 它正在工作,但这是片段的变化。我可以从 MainActivity.java 更改它吗?
    【解决方案2】:

    在您的 xml 中,您应该有一个片段容器。假设这个ID是fragment_id

    对于支持库,

    BlankFragment blankFrag = (BlankFragment)getSupportFragmentManager().
                                                  findFragmentById(R.id.fragment_id);
    

    否则对于不支持的库Fragment

    BlankFragment blankFrag = (BlankFragment)getFragmentManager().
                                                 findFragmentById(R.id.fragment_id); 
    

    您还需要BlankFragment 类中的公共方法,其签名类似于setTextViewText(String text),然后从MainActivity 调用它,例如blankFrag.setTextViewText("my text");

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      相关资源
      最近更新 更多