TextView visibility change notification

十年热恋 提交于 2020-04-11 12:44:26

问题


I need to detect when a TextView is made visible and when its made invisible. Is there any way to do this in Android? I've to make some processing when a TextView in my application changes visibility. Actually, it's visibility is being updated at a number of places and I'm looking to avoid calls at all of these places.


回答1:


I don't know if this will answer your question, but if you want to listen to textview visibility changes, I suggest customizing TextView and implement your own listener for it.

 public class TextViewExtension extends TextView {


     protected OnVisibilityChange mChangeListener = null;

     public interface OnVisibilityChange {
         void onChange(TextViewExtension mTextView, int mPrevVisibility, int mNewVisibility);
     }

     public TextViewExtension(Context context) {
         super(context);
         // TODO Auto-generated constructor stub
     }

     /* (non-Javadoc)
      * @see android.view.View#setVisibility(int)
      */
     @Override
     public void setVisibility(int visibility) {
         // TODO Auto-generated method stub
         super.setVisibility(visibility);
         if (mChangeListener != null) {
             mChangeListener.onChange(this, getVisibility(), visibility);
         }
     }

     public void setOnVisibilityChange(OnVisibilityChange mChangeListener) {
         this.mChangeListener = mChangeListener;
     }
 }

Here are examples for further implementation if you are curious

Hope it helps :)




回答2:


visibility="gone" 

in your xml fil




回答3:


static boolean textviewon=false;
if(textviewon){
textview.setvisibility(View.Visible);
}else
textview.setvisibility(View.Invisible);

put above condition in your code



来源:https://stackoverflow.com/questions/25153665/textview-visibility-change-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!