当我尝试在片段中动态更改TextView值时没有任何反应

时间:2019-02-28 21:19:19

标签: android

我尝试了Internet上的所有内容,但仍无法更新该值。但是我可以更新进度条的值。我想知道为什么我可以更新进度,但不能更新textview值

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ProgressBar prograss_bar;
        int prograss = 28;
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);
        TextView prograss_perc = (TextView) view.findViewById(R.id.sale_prog_pres);
        prograss_perc.setText(String.valueOf(prograss));
        prograss_bar = (ProgressBar) view.findViewById(R.id.sales_prog);
        prograss_bar.setProgress(prograss);


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


    }

我得到的Textview值为空,但是进度条更新了。我能做什么?

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".tab1">
    <TextView
        android:id="@+id/sale_prog_pres"
        android:layout_width="84dp"
        android:layout_height="29dp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@+id/sales_prog"
        app:layout_constraintEnd_toEndOf="@+id/sales_prog"
        app:layout_constraintStart_toStartOf="@+id/sales_prog"
        app:layout_constraintTop_toTopOf="@+id/sales_prog" />
    <ProgressBar
        android:id="@+id/sales_prog"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="247dp"
        android:layout_height="212dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:progressDrawable="@drawable/circle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.396" />

    <TextView
        android:id="@+id/rights"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="Powered by Sinewavesoft "
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:2)

您的代码有2个问题。

首先,通过添加android:textColor="#FFF"确保文本颜色与背景颜色不同。

<TextView
      android:id="@+id/sale_prog_pres"
      android:layout_width="84dp"
      android:layout_height="29dp"
      android:gravity="center"
      android:background="@android:color/black"
      android:textColor="#FFF" 
      app:layout_constraintBottom_toBottomOf="@+id/sales_prog"
      app:layout_constraintEnd_toEndOf="@+id/sales_prog"
      app:layout_constraintStart_toStartOf="@+id/sales_prog"
      app:layout_constraintTop_toTopOf="@+id/sales_prog" />

第二,将onCreateView方法代码更改为

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_main, container, false);

      ProgressBar prograss_bar;
      int prograss = 28;
      TextView prograss_perc = (TextView) view.findViewById(R.id.sale_prog_pres);
      prograss_perc.setText(String.valueOf(prograss));
      prograss_bar = (ProgressBar) view.findViewById(R.id.sales_prog);
      prograss_bar.setProgress(prograss);

      // This line is IMPORTANT!!!
      return view;
}

答案 1 :(得分:1)

根据您提供的代码片段,将TextView prograss_perc的文本值设置为对象本身的字符串值。应该是:

int progress = 28;
prograss_perc.setText(String.valueOf(progress));

编辑:TextView位于ProgressBar的后面。改用它。

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
                                             android:layout_height="match_parent"
tools:context=".tab1">
    <ProgressBar
            android:id="@+id/sales_prog"
            android:background="@android:color/holo_red_dark"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="247dp"
            android:layout_height="212dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:progressDrawable="@drawable/circle"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.396" />

    <TextView
            android:id="@+id/sale_prog_pres"
            android:layout_width="84dp"
            android:layout_height="29dp"
            android:gravity="center"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="@+id/sales_prog"
            app:layout_constraintEnd_toEndOf="@+id/sales_prog"
            app:layout_constraintStart_toStartOf="@+id/sales_prog"
            app:layout_constraintTop_toTopOf="@+id/sales_prog" />

    <TextView
            android:id="@+id/rights"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="Powered by Sinewavesoft "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>