【发布时间】:2017-09-04 09:56:50
【问题描述】:
我知道以前有人问过类似的问题,但是我找不到解决我问题的线程。
我有两个重叠的元素(布局和按钮),我通过回调方法更改其可见性。奇怪的是,它第一次起作用,但是如果我第二次尝试它就不起作用。当我单击布局中的按钮时,布局设置为View.GONE,另一个按钮btn 设置为View.VISIBLE。当我单击btn 按钮时,会启动另一个活动,并从该活动再次启动此活动。那是无法再次切换可见性的时候,但是当我重新启动整个应用程序时,它第一次工作。
我还使用了处理程序而不是runOnUiThread(..),但它仍然不起作用。
我还检查了可见性的状态,统计是可见的,但它仍然没有显示。
这是我的代码:
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();
}
}
这是我的布局:
<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"
/>
这是下一个活动:
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"));
}
}
【问题讨论】:
-
不要使用重叠按钮,而是使用单个按钮并在单击后更改侦听器。
-
@ShreyashSSarnayak 问题是,我实际上有两个按钮相邻,然后另一个按钮与这两个按钮重叠,所以你的解决方案不起作用
-
您如何开始其他活动并重新开始当前活动?
-
@MoQ93 我编辑了代码,你可以看看我现在是如何开始活动的
-
@MerveSahin 我刚刚尝试了您的代码,它对我有用。我已经用在 doInBackground 中调用 myCallback() 的 asyncTask 对其进行了测试。我还用 FrameLayout 包装了你的布局,因为它是无效的
标签: android callback toggle visibility