2017-11-21 13 views
0

TableLayoutに問題があります。私は "クリア"ボタンをクリックするとその内容をすべて隠そうとしています。ただし、ボタンを2回クリックしたときにのみ機能します。私はまた、このテーブルレイアウトを隠す、または表示する2つのラジオボタンを持っています。奇妙なことは、まったく同じコードのラジオボタンをクリックしたときに動作しているということです:setVisibility(View.GONE)setVisibility(View.VISIBLE)TableLayout可視性ボタン上の2回目のクリックでのみ動作します

私を助けることができれば、それはすばらしいでしょう。ここで

は、コードは次のとおりです。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.dialog_task_validation); 

     setTitle("Task Validation"); 

     // set the custom dialog components - text, image and button 
     final RadioGroup radioGroupTask = (RadioGroup) findViewById(R.id.radioGroup); 
     radioGroupTask.clearCheck(); 

     noteLayout = (TableLayout) findViewById(R.id.noteLayout); 


     radioGroupTask.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 
       RadioButton rb = (RadioButton) group.findViewById(checkedId); 
       if (rb != null && checkedId > -1) { 
        switch (checkedId) { 
         // If task Failed 
         case R.id.taskFailedRadioButton: 
          taskCompeleted = 0; 
          noteLayout.setVisibility(View.GONE); 
          note = 0; 
          break; 
         case R.id.taskCompletedRadioButton: 
          taskCompeleted = 1; 
          noteLayout.setVisibility(View.VISIBLE); 
          break; 
        } 
       } 

      } 
     }); 
     Button clear = (Button) findViewById(R.id.clearDialogButton); 


     clear.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       noteLayout.setVisibility(View.GONE); 
       resetButtonBackground(); 
       note = -1; 
       taskCompeleted = -1; 
       radioGroupTask.clearCheck(); 
      } 
     }); 
    } 

とXML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="5dp"> 

    <RadioGroup 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_below="@+id/recordNameTV" 
     android:id="@+id/radioGroup"> 

     <RadioButton 
      android:id="@+id/taskFailedRadioButton" 
      style="?android:attr/radioButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/task_failed" /> 
     <RadioButton 
      android:id="@+id/taskCompletedRadioButton" 
      style="?android:attr/radioButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/task_completed" /> 
    </RadioGroup> 


    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/noteLayout" 
     android:layout_below="@+id/radioGroup" 
     android:layout_above="@+id/linearLayout" 
     android:visibility="gone"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      <Button 
       android:id="@+id/note1" 
       android:text="1" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
      <Button 
       android:id="@+id/note2" 
       android:text="2" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
      <Button 
       android:id="@+id/note3" 
       android:text="3" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <Button 
       android:id="@+id/note4" 
       android:text="4" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
      <Button 
       android:id="@+id/note5" 
       android:text="5" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
      <Button 
       android:id="@+id/note6" 
       android:text="6" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <Button 
       android:id="@+id/note7" 
       android:text="7" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
      <Button 
       android:id="@+id/note8" 
       android:text="8" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
      <Button 
       android:id="@+id/note9" 
       android:text="9" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <Button 
       android:id="@+id/note10" 
       android:text="10" 
       android:layout_weight="1" 
       style="?android:attr/buttonBarButtonStyle"/> 
     </TableRow> 
    </TableLayout> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_alignParentBottom="true" 
      android:id="@+id/linearLayout"> 

      <Button 
       android:id="@+id/clearDialogButton" 
       style="?android:attr/buttonBarButtonStyle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/clearText" /> 

     </LinearLayout> 
</RelativeLayout> 

ありがとう!

+3

ボタンクリックリスナーからclearChecks()を削除しようとしましたか? –

+1

'radioGroupTask.clearCheck();'は 'onCheckedChange'コールバックをトリガし、あなたのレイアウトは再び変更されます。 – chrjs

+0

ああそうだよ!以前にclearCheck()を追加しました。うまくいきました!どうも ! – Maxouille

答えて

2

ちょうど私がここに私のコメント報告するつもりです他の人を助けるために:それはあなたのradioGroupTaskonCheckedChangeリスナーをトリガーするため

button click listenerclearCheckコールを削除する(または可視性を変更する前に、それを動かす)をこれにより、layoutが再び変更されます。

2

clearCheck() - 選択をクリアします。最初に選択をクリアする必要があります。まずTableLayoutを非表示にする必要があります。

関連する問題