2017-04-09 1 views
0

私は以前同様の質問があったことは知っていますが、私の問題を解決したスレッドは見つかりませんでした。 私は2つの重なり合った要素(LayoutとButton)を持っていますが、コールバックメソッドで可視性を変更しています。奇妙なことは、初めて動作するのですが、2回目に試しても動作しません。レイアウトのボタンをクリックすると、レイアウトはView.GONEに設定され、他のボタンbtnView.VISIBLEに設定されます。 btnボタンをクリックすると、別のアクティビティが開始され、そのアクティビティからこのアクティビティが再び開始されます。可視性をもう一度切り替えることはできませんが、アプリケーション全体を再起動すると初めて動作します。 runOnUiThread(..)の代わりにハンドラも使用しましたが、それでも動作しませんでした。 私は可視性の状態もチェックしましたが、統計は可視性ですが、まだ表示されていません。setVisibility(int)コールバックで2回目が動作しない

これは私のコードです:

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")); 
    } 
} 
+0

重複するボタンを使用する代わりに、1つのボタンを使用し、クリックした後にリスナーを変更します。 –

+0

@ShreyashSSarnayak事実上、私は実際にはお互いに隣り合った2つのボタンと、これらのボタンの両方が重なっている別のボタンがあるので、あなたの解決策は機能しません。 –

+0

あなたは他のアクティビティをどのように開始していますか? – MoQ93

答えて

0

私はついにそれを解決しました。この問題は、myCallbackメソッドを呼び出すスレッドにあります。私は同じスレッドを再利用していました。コールバックメソッドでスレッドをnullに設定してこれを修正しました。

0

それがあったとしてFirstActivityは、すべての可視性を返す離れる前に意味...

FirstActivityを離れる前にこれら2行を追加してください

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

残念ながら、これはうまくいきませんでしたが、もう一度動作しませんでした。 –

関連する問題