简体   繁体   English

setVisibility(int)在回调中第二次不起作用

[英]setVisibility(int) not working second time in a callback

I know similar questions have been asked before, however I could not find a thread that solved my problem.我知道以前有人问过类似的问题,但是我找不到解决我问题的线程。 I have two overlapping elements (Layout and Button), on which I change the visibility through a callback method.我有两个重叠的元素(布局和按钮),我通过回调方法更改其可见性。 The weird thing is, it works the first time, however if I try it the second time it doesn't work.奇怪的是,它第一次起作用,但是如果我第二次尝试它就不起作用。 When I click the button in the layout, the layout is set to View.GONE and the other button btn is set to View.VISIBLE .当我单击布局中的按钮时,布局设置为View.GONE ,另一个按钮btn设置为View.VISIBLE When I click the btn Button another Activity is started and from that activity this Activity is started again.当我单击btn按钮时,会启动另一个 Activity,然后从该活动中再次启动该 Activity。 That's when the visibility cannot be toggled again, but when I restart the entire application it works the first time.那是无法再次切换可见性的时候,但是当我重新启动整个应用程序时,它会第一次工作。 I also used a Handler instead of runOnUiThread(..) and it still did not work.我还使用了 Handler 而不是runOnUiThread(..) ,但它仍然无法正常工作。 I also checked the state of the visibility, the stat is visibile, however it is still not shown.我还检查了可见性的状态,统计数据是可见的,但它仍然没有显示。

This is my code:这是我的代码:

public class FirstActivity extends AppCompatActivity{

private LinearLayout buttonLayout;
private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ..
    buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
    btn = (Button) findViewById(R.id.btn);
}

@Override
public void myCallback(int n){

   this.runOnUiThread(() -> {
       buttonLayout.setVisibility(View.GONE);
       btn.setVisibility(View.VISIBLE);

   });       
}

   public void onClick1(View view){
       Intent intent = new Intent(this, Result.class);
       startActivity(intent);
       finish();
   }
}

This is my layout:这是我的布局:

<LinearLayout
android:id="@+id/buttonLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="300px"
android:weightSum="1">

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="x"
    android:textColor="@android:color/white"
    android:id="@+id/testNoBtn"
    android:onClick="onClick1"
    android:background="#f44336"
    android:layout_weight="0.45"
    android:enabled="false"
    android:visibility="visible"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="✔"
    android:textColor="@android:color/white"
    android:id="@+id/yesBtn"
    android:onClick="onClick2"
    android:background="#4caf50"
    android:layout_weight="0.45"
    />

</LinearLayout>

<Button
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="see result"
    android:background="#03a9f4"
    android:textColor="@android:color/white"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="300px"
    android:onClick="onContinueClicked"
    android:visibility="invisible"
     />

This is the next Activity:这是下一个活动:

public class Result extends AppCompatActivity{

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

    }

    public void tryAgain(){

        startActivity(new Intent("android.intent.action.FirstActivity"));
    }
}

before leaving FirstActivity return every visibility as it was meaning ...在离开 FirstActivity 之前返回所有可见性,因为它的意思......

add these two lines before leaving FirstActivity在离开 FirstActivity 之前添加这两行

btn.setVisibility(View.INVISIBLE);
buttonLayout.setVisibility(View.VISIBLE);

I finally solved it.我终于解决了。 The problem was lying in the thread calling the myCallback method.问题在于调用myCallback方法的线程。 I was reusing the same thread, it would stay idle until I started it internally again.我正在重用同一个线程,它会保持空闲状态,直到我再次在内部启动它。 I fixed this by setting the thread to null in the callback method.我通过在回调方法中将线程设置为 null 来解决此问题。

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

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