Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从另一个文本片段更新ViewPager中的文本视图_Java_Android_Android Fragments - Fatal编程技术网

Java 从另一个文本片段更新ViewPager中的文本视图

Java 从另一个文本片段更新ViewPager中的文本视图,java,android,android-fragments,Java,Android,Android Fragments,我创建了一个包含3个片段的选项卡式活动。如何更新MainActivity Java文件片段中TextView的文本 TabStatus.java: package com.sanderjochems.bluetoothdata; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.Layo

我创建了一个包含3个片段的选项卡式活动。如何更新MainActivity Java文件片段中TextView的文本

TabStatus.java:

package com.sanderjochems.bluetoothdata;

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

public class TabStatus extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_status, container, false);
    }
}
fragment_status.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sanderjochems.bluetoothdata.MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="24dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/title_bluetooth"
                android:textAllCaps="false"
                android:textColor="@color/colorPrimaryText"
                android:textSize="24sp"
                android:textStyle="bold" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="8dp" />

            <TextView
                android:id="@+id/tbMacAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/text_mac_address" />

            <TextView
                android:id="@+id/tbState"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/text_state" />

        </LinearLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>
问候,,
Sander Jochems

在片段中执行操作时使用事件总线并触发事件,然后在另一个片段中捕获该事件并更新所需的textview

遵循这个非常简单的库实现和使用

请跟随

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutStatus = inflater.inflate(R.layout.fragment_status, null);

tvCurrentState = (TextView) layoutStatus.findViewById(R.id.tbState);