2015-10-28 2 views
1

Текстовое изображение должно быть видимым при установке флажка. Однако этого не происходит. Когда я нажимаю на флажок, он становится установленным, но texview не появляется. Я установил видимость текстового представления GONE в XML. Тем не менее, я устанавливаю его в VISIBLE, когда флажок установлен.Флажок android не работает

View footer = inflater.inflate(R.layout.footer, viewGroup, 
        false); 


    final CheckBox chb = (CheckBox) footer.findViewById(R.id.notify_confirm); 
       final TextView notify_tv = (TextView) footer.findViewById(R.id.notify_tv); 



       chb.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

       if (chb.isChecked()) 
       { 
        //new CheckBoxThread().execute(); 
         notify_tv.setVisibility(View.VISIBLE);  

       } 
       else 
       { 

       } 

       } 

      }); 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
           android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
       android:paddingLeft="6dp" 
       android:orientation="vertical" 
        android:background="#ffffff" 
     > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:visibility="visible" 

     > 

     <CheckBox 
      android:id="@+id/notify_confirm" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:text="Notify on Confirmation" 
      android:textColor="#4d4d4d" 
      android:paddingLeft="5dp" 
      android:visibility="visible" 

      /> 
     <TextView 
      android:id = "@+id/notify_tv" 
      android:layout_width="fill_parent" 
      android:textColor = "@color/myPrimaryColor" 
      android:gravity="center" 
      android:visibility="gone" 
      android:layout_height="wrap_content" 
      android:text="You will be notified on confirmation"></TextView> 

    </LinearLayout> 
</LinearLayout> 

Это может быть немного наивным меня, чтобы задать этот вопрос, но я пробовал все, и он просто не похоже на работу.

+1

Используйте '' OnCheckedChangeListener' для CheckBox '. – Piyush

ответ

2

Вместо использования OnClickListener используйте OnCheckedChangeListener для флажков. Замените код, как показано ниже.

chb.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     if (isChecked) 
     { 
      // perform logic 
      notify_tv.setVisibility(View.VISIBLE);  
     } 

    } 
}); 
+0

Да, это сработало. Благодарю. –

+0

Отлично .. @Div_D .. Не могли бы вы отметить ответ как принятый, если это действительно помогло вам .. – Lal

0
Add this property to the checkbox widget android:onClick="onCheckboxClicked"/> 

Within the Activity that hosts this layout, the following method handles the click event for both checkboxes: 

public void onCheckboxClicked(View view) { 
    // Is the view now checked? 
    boolean checked = ((CheckBox) view).isChecked(); 

    // Check which checkbox was clicked 
    switch(view.getId()) { 
     case R.id.notify_confirm: 
      if (checked) 
       //visible the textview here 
      else 
       // 
      break; 
0

По пути с onClickListener:

chkBox.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
       //is chkIos checked? 
     if (((CheckBox) v).isChecked()) { 
      Toast.makeText(MyAndroidAppActivity.this, 
       "Bro, try Android :)", Toast.LENGTH_LONG).show(); 
     } 
else{ 
//do else work 
} 

     } 
    }); 

Другое и Лучший способ заключается в использовании:

CompoundButton.OnCheckedChangeListener: 

Как описано г-н Лал.

Благодаря

0

Я проверил ваш код, это было прекрасно, но только проблема немного ориентации (Layout ориентация) была там. и не используйте TextView: видимость: «ушел» (поскольку ушедший обновит ваш взгляд, и ваше представление будет разбросано) скорее используйте «невидимый», это не повлияет на ваше мнение.

только что поставил андроид: ориентация = «вертикальная» на вашем втором LinearLayout.

здесь ваш измененный файл макета:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" 
    android:orientation="vertical" 
    android:paddingLeft="6dp" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:visibility="visible" 
     android:orientation="vertical"> 

     <CheckBox 
      android:id="@+id/notify_confirm" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:paddingLeft="5dp" 
      android:text="Notify on Confirmation" 
      android:textColor="#4d4d4d" 
      android:visibility="visible" /> 

     <TextView 
      android:id="@+id/notify_tv" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:gravity="center" 
      android:text="You will be notified on confirmation" 
      android:textColor="#000000" 
      android:visibility="invisible" > 
     </TextView>  
    </LinearLayout> 
</LinearLayout> 

и вот ваш Java файл:

final CheckBox chb = (CheckBox) findViewById(R.id.notify_confirm); 
final TextView notify_tv = (TextView)findViewById(R.id.notify_tv); 
     chb.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
      if (chb.isChecked()) 
      { 
       //new CheckBoxThread().execute(); 
        notify_tv.setVisibility(View.VISIBLE);  
      } 
      else 
      { 
       notify_tv.setVisibility(View.INVISIBLE); 
      } 
      } 
     }); 

это будет работать ... :)

Смежные вопросы