setVisibility无法正常工作android

时间:2017-08-09 09:08:00

标签: android android-view visibility

我正在隐藏一个布局说layoutA并显示另一个布局,比如layoutB使用以下代码:

layoutA.setVisibility(View.GONE);
layoutB.setVisibility(View.VISIBLE);

layoutB有一个图像进度条,由计时器控制。问题有时,隐藏的布局被正确隐藏。有时,layoutA覆盖的layoutB部分尽管被隐藏但仍会闪烁。它仍然可见。有时,它仍然可见。

这是我的代码:

layoutA.setVisibility(View.GONE);
layoutB.setVisibility(View.VISIBLE);

tmrOTP = new CountDownTimer(60000, 500) {

    public void onTick(long millisUntilFinished) {
        final int progressToSet = (int) (((60000 - millisUntilFinished)*100)/60000);

        if(!numVerified) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    updateProgressBar(progressToSet);
                }
            });
        }
        else {
            layoutA.setVisibility(View.VISIBLE);
            layoutB.setVisibility(View.GONE);
            tmrOTP.cancel();
        }
    }

    public void onFinish() {
        layoutA.setVisibility(View.VISIBLE);
        layoutB.setVisibility(View.GONE);
        tmrOTP = null;
    }

}.start();

这是我的xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutA"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="319dp"
android:layout_height="287dp"
......
......>


<EditText
        android:id="@+id/txtMobile"
        android:layout_width="300dp"
        android:layout_height="55dp"
        android:ems="10"
        android:inputType="number"
        android:background="@drawable/edittext_style_g"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintHorizontal_bias="0.0" />



     <Button
         android:id="@+id/btnSendOTP"
         android:layout_width="300dp"
         android:layout_height="55dp"
         android:textSize="23sp"
         android:text="Send OTP"
         android:textColor="#ffffff"
         android:background="@drawable/button_style_g"
         android:layout_marginRight="8dp"
         app:layout_constraintRight_toRightOf="parent"
         android:layout_marginLeft="8dp"
         app:layout_constraintLeft_toLeftOf="parent"
         android:layout_marginTop="35dp"
         app:layout_constraintTop_toBottomOf="@+id/txtMobile"
         android:layout_marginStart="8dp"
         android:layout_marginEnd="8dp" />

</android.support.constraint.ConstraintLayout>



<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutB"
    android:visibility="gone"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="319dp"
    android:layout_height="287dp"
    ......
    ......>


<ImageView
        android:id="@+id/imgProgress"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="33dp"
        android:background="@null"
        ....
        .... />

</android.support.constraint.ConstraintLayout>

这是layoutA

this is layoutA

这是布局B-注意:此处除了进度条所涵盖的部分之外,布局A是隐藏的

this is layoutB

0 个答案:

没有答案