您的位置:首页 > 移动开发 > Android开发

Android ViewTreeObserver

2017-03-20 16:41 239 查看
view变化监听器ViewTreeObserver介绍

<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:id = "@+id/full_screen"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent">

< TextView
android:id = "@+id/tv_show"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text = ""
android:textSize = "32px"
android:textColor = "#FFFF00"/>
< EditText
android:id = "@+id/ed_enter1"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text = ""/>
< EditText
android:id = "@+id/ed_enter2"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text = ""/>
< TextView
android:id = "@+id/tv_display"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text = ""/>
< Button
android:id = "@+id/button"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text = "OK"/>
</ LinearLayout >


注意:给 layout 增加一个 id : full_screen

2、Activity 对应的 Java 代码如下:

public class ControlViewTreeObserver extends Activityimplements
OnClickListener,
ViewTreeObserver.OnTouchModeChangeListener,          // 用于监听 Touch 和非 Touch 模式的转换
ViewTreeObserver.OnGlobalLayoutListener,             // 用于监听布局之类的变化,比如某个空间消失了
ViewTreeObserver.OnPreDrawListener,                  // 用于在屏幕上画 View 之前,要做什么额外的工作
ViewTreeObserver.OnGlobalFocusChangeListener         // 用于监听焦点的变化
{
private TextView tv_show ;
private ViewTreeObserver vto ;
private View all ;
private EditText ed1 ;
private EditText ed2 ;
private TextView tv_display ;
private Button button ;
private boolean btnClicked ;

@Override
public void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout. main );
tv_show = (TextView) this .findViewById(R.id. tv_show );
// 得到整个屏幕对象 , 因为顶层 layout 的 width 和 height 都是 fill_parent
all = this .findViewById(R.id. full_screen );
// 通过 getViewTreeObserver 获得 ViewTreeObserver 对象
vto = (ViewTreeObserver) all .getViewTreeObserver();
tv_display = (TextView) this .findViewById(R.id. tv_display );
ed1 = (EditText) this .findViewById(R.id. ed_enter1 );
ed2 = (EditText) this .findViewById(R.id. ed_enter2 );
button = (Button) this .findViewById(R.id. button );
button .setOnClickListener( this );
// 增加对应的 Listener
vto .addOnTouchModeChangeListener( this );
// 增加对应的 Listener
vto .addOnGlobalFocusChangeListener(this );
// 增加对应的 Listener
vto .addOnPreDrawListener( this );
// 增加对应的 Listener
vto .addOnGlobalLayoutListener( this);
}

/**
*  onTouchModeChanged 是接口 ViewTreeObserver.OnTouchModeChangeListener
* @param isInTouchMode
*/
@Override
public void onTouchModeChanged( boolean isInTouchMode){
if (isInTouchMode){
tv_show .setText( "In touch mode" );
}else {
tv_show .setText( "Not in touch mode" );
}
}

/**
* onGlobalLayout 是接口 ViewTreeObserver.OnGlobalLayoutListener中定义的方法。
* Callback method to be invokedwhen the global layout state or the
* visibility of views within the view treechanges
*/
@Override
public void onGlobalLayout(){
if ( btnClicked ){
if (! ed2 .isShown()){
ed1 .setText( " 第二个 EditText 不见了 " );
}else{
ed1 .setText( " 第二个 EditText 出来了 " );
}
}
}

/**
* onPreDraw 是接口 ViewTreeObserver.OnPreDrawListener中定义的方法。
* @return
*/
@Override
public boolean onPreDraw(){
// 在屏幕上画出 ed1 控件之间 , 给它增加一个提示 , 并改变其字体大小
ed1 .setHint( " 在 onPreDraw 方法中增加一个提示信息 " );
ed1 .setTextSize(( float ) 20.0);
//return false;   // Return true to proceed with the current drawing pass, or falseto cancel.
return true ;       // 如果此处不返回 true , 则整个界面不能完整显示。
}

/**
* onGlobalFocusChanged 是接口 ViewTreeObserver.OnGlobalFocusChangeListener中定义的方法。
* 焦点发生变化时,会触发这个方法的执行
* @param oldFocus
*/
@Override
public void onGlobalFocusChanged(View oldFocus, ViewnewFocus){
if (oldFocus != null && newFocus!= null ){
tv_display .setText( "Focus /nFROM:/t" + oldFocus.toString() + "/n TO:/t" + newFocus.toString());}
}

@Override
public void onClick(View v){
// 改变 ed2 的可见性 , 会触发 onGlobalLayout 方法的执行
btnClicked = true ;
if (v.getId() == R.id. button ){
if (ed2 .isShown()){
ed2 .setVisibility(View. INVISIBLE );
}else{
ed2 .setVisibility(View. VISIBLE );
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android ViewTreeOb